In my previous article, we discussed Models in Asp.Net MVC. In Asp.Net MVC, View is responsible for displaying the Model or Models into a visual representation. View shows the model in HTML format, but it can display data in PDF, XML, CSV, and Excel,…etc. The main and only objective of View is just to display model, and it does not contain any business logic.
Controller returns an instance of ViewResult type of ActionResult which knows how to render content to the response. Views store in Views folder in Asp.Net MVC. Asp.Net MVC searches for the view with the help of name provided by the Controller. For example Home Controller Index() method returns the View as shown below.
public ActionResult Index()
{
ViewBag.Message = “This is Home View”;
return View();
}
Above Index() method searches for the view Home in the Views folder. Here View() method used to create a ViewResult. By calling View() method instructs Asp.Net MVC to search view with the same name as current controller Home. Views should be stored in the root folder Views with the name of Controllers. In the above example, the view should be stored in Views/Home folder. When Asp.Net MVC does not find the view in the Controller’s views folder, it searches entire Views folder. The view contains not only HTML markup but also it is a Razor. Please find below for the basic razor syntax.
<ul>
@foreach(var employee in Employees){
<li>@employee.Name</li>
}
</ul>
As shown above, we are displaying code in HTML by using razor syntax @ symbol in the view. We will learn more about Razor code in future articles.
We have two options to differentiate code from HTML markup in Asp.Net MVC; those are Code nuggets and Code blocks.
Code nuggets: Code nuggets are used to insert the code inline within the HTML by using @ symbol.
Find all employees information at: @Html.ActionLink(“Employees”, employees)
The code starts immediately after the @ symbol, Razor is ingenious and identifies the closing parenthesis indicates the end of the statement. Code nugget always returns markup to render. If we write the code nugget which returns the void value, it returns the errors.
Code block: A code block in Asp.Net MVC is a section of the view that should contain only code and it should not contain markup. Razor defines code block in @{ } characters. @{ is the start of the code block and } is the end of the code block as shown below.
@{
LayoutPage = “~/Views/Shared/_Layout.cshtml”;
View.Title = “This is the Layout page”;
}
Layouts: In Asp.Net MVC we do not have master pages like in Asp.Net to maintain common layout across a web application. We have Layouts in Asp.Net MVC to maintain common layout across a web application. The layout contains scripts, CSS, URLs,.., etc which are common for all web applications. In Asp.Net MVC, layouts are views and stores in Views/Shared folder. We discuss more layouts in next articles.
Partial Views: Partial views are like Asp.Net web user controls used to contain reuse portions of markup. Partial views stores in Views/Shared folder. We discuss more about partial views in future articles.