Monday, April 20, 2015

Creating a View in Asp.Net MVC

9:28 PMMonday, April 20, 2015
Creating a View means adding an HTML file for an existing action method in controller. View is the page that is shown to user, whatever HTML you write or CSS you embed is fully applicable for that view.
In earlier article we have created a controller having an “Index” action method which is by default added. We have changed this action method and returns a string to be displayed on browser directly.
When working with views programmer should know about HTML language and should have a little bit knowledge of CSS style-sheets. In this article we will create a view page checking the options that can be applied on creating a view.
Change index action method as it was when created:
public ActionResult Index()
return View();
Right click on Index and click on Add View. It will show Add View dialog box having a list of options as in below screenshot. We will discuss all these options in later articles, by now just click on Add button.
Right click on Index and click on Add View

Expand the Views folder, it have a new folder having the same name of our controller i.e. Home. All the views created through this controller will be saved in this folder. Open the newly created view, it have only a bit code as shown:
Open the newly created view

Change this code and insert an hyperlink <a href="">Code Walls</a>. Run this project and it will default show this page.
Change this code and insert an hyperlink

Data on View from Controller
When we create an MVC application as “Internet Application” template then by default Index action method have a Viewbag message defined as shown below:
public ActionResult Index()
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

return View();
Change this message or leave as is (I have changes this message a little bit) and in the view page there is a line showing the output of this viewbag message i.e.
Run this project and check the message on the page as shown in green ellipse. You can change this in-built template according to your own design scenario. Using ViewBag you can dynamically share the values from controller to view, we will discuss more about it later.
Creating a View in Asp.Net MVC

Protected by Copyscape Online Copyright Protection Software


Post a Comment

Toggle Footer