In Focus

Views In ASP.NET MVC

In this article, you will learn about Views in ASP.NET MVC.

Introduction

 
This article will explain the concept of Views in ASP.NET MVC application with an example.
 

Views in ASP.NET MVC

 
In the MVC pattern, the View component contains the logic to represent the model data as a user interface with which the end user can interact. Typically, it creates the user interface with the data from the model which is provided to it by the Controller in an MVC application. So, you can consider the Views in ASP.NET MVC as HTML templates embedded with Razor syntax which generate HTML content that it sends to the client.
 

Where are ASP.NET MVC View Files stored?

 
In ASP.NET MVC, the Views are having .cshtml extension when we use C# as the programming language with Razor syntax. Usually, each controller will have its own folder in which the controller-specific .cshtml files (views) are going to be saved. The controller-specific folders are going to be created within the Views folder. The main point that you need to keep in mind is the view file name and the controller action name are going to be the same.
 
Step 1
 
Open Visual Studio 2015 or a version of your choice and create a new project.
 
Step 2
 
Choose the web application project and give an appropriate name for your project.
 
Views In ASP.NET MVC
 
Step 3
 
Select MVC template below and click OK.
 
Views In ASP.NET MVC
 
Let’s say, we created an ASP.NET MVC application with two controllers, i.e., EmployeeController and HomeController. The HomeController that we created is having the following three action methods.
  1. About()
  2. Contact()
  3. Index()
Similarly, the EmployeeController is created with the following four action methods.
  1. Index()
  2. Details()
  3. Create()
  4. Edit()
  5. Delete()
Step 4
 
Right-click on Controllers folder and add a controller.
 
Views In ASP.NET MVC
 
A window will appear. Choose MVC5 Controller-Empty and click "Add".
 
Views In ASP.NET MVC
 
After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home,
 
Views In ASP.NET MVC
 
Similarly, add "EmployeeController".
 
Views In ASP.NET MVC
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.    
  7. namespace MvcViews_Demo.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         // GET: Home  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.    
  17.         public ActionResult About()  
  18.         {  
  19.             return View();  
  20.         }  
  21.    
  22.         public ActionResult Contact()  
  23.         {  
  24.             return View();  
  25.         }  
  26.     }  
  27. }  
Views In ASP.NET MVC
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6.      
  7. namespace MvcViews_Demo.Controllers    
  8. {    
  9.     public class EmployeeController : Controller    
  10.     {    
  11.         // GET: Employee    
  12.         public ActionResult Index()    
  13.         {    
  14.             return View();    
  15.         }    
  16.      
  17.         public ActionResult Details()    
  18.         {    
  19.             return View();    
  20.         }    
  21.      
  22.         public ActionResult Create()    
  23.         {    
  24.             return View();    
  25.         }    
  26.      
  27.         public ActionResult Edit()    
  28.         {    
  29.             return View();    
  30.         }    
  31.      
  32.         public ActionResult Delete()    
  33.         {    
  34.             return View();    
  35.         }    
  36.     }    
  37. }    
The Views are going to be created and saved in the following order.
 
Views In ASP.NET MVC
 
As we have two controllers in our application, so there are two folders created with the Views folder one per controller. The Home Folder is going to contain all the view files (i.e. cshtml files) which are specific to HomeController. Similarly, the Employee Folder is going to contain all the .cshtml files which are specific to Employee Controller. This is the reason why the Home folder contains the Index, “About” and “Contact” cshtml files. Similarly, the Employee folder contains the Index, Details, Create, Edit, and Delete view files.
 

Understanding Views in MVC with Examples

 
To understand the views in ASP.NET MVC applications, let us first modify the HomeController as shown below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.    
  7. namespace MvcViews_Demo.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         // GET: Home  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }  
In the above HomeController, we created an Action method which is going to return a view. In order to return a view from an action method in MVC, we need to use the View() extension method which is provided by System.Web.Mvc.Controller Base class.
 
Now run the application and navigate to the “/Home/Index” URL and you will get the following error.
 
Views In ASP.NET MVC
 
Let us understand why we got the above error.
 
As we are returning a view from the Index action method of Home Controller, by default the MVC Framework will look for a file with the name Index.aspx or Index.ascx within the Home and Shared folder of the application if the view engine is APSX. If it is not found there then it will search for a view file with the name Index.cshtml or Index.vbhtml within the Home and Shared folder of the application.
 
If the requested view file is found in any of the above locations, then the View generates the necessary HTML and sends the generated HTML back to the client who initially made the request. On the other hand, if the requested view file is not found in any of the above locations, then we will get the above error.
 
Step 5
 
Right-click on Index method in HomeController the "Add View" window will appear with the default index name checked (use a Layout page), and click on "Add.
 
Views In ASP.NET MVC
 
Code for Index View
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.    
  5. <h2>Hello World, Welcome to MVC(Model View Controller)</h2>  

Advantages of Using Views

 
The Views in ASP.NET MVC application provides the separation of concerns (codes). It means, it separates the user interface from the rest of the application such as business layer and data access layer. The ASP.NET MVC views use the advanced Razor syntax which makes it easy to switch between the HTML and C# code. The common or repetitive sections of the webpages can be easily reused between by using layout and partial views.