Views In ASP.NET MVC

What is a View in ASP.NET MVC?

View in ASP.NET MVC represents the user interface of the Application. Views are present in the views folder of the Application. We can send the data to the view from controller, using ViewBag and ViewData but strongly typed Views are the best practices to send the data from Controller to the View and strongly typed Views are not the part of this article.

  • View name is same as an action method name in the controller class.
  • Razor Views are more readable and best practice to be used in the View.
  • View ends with .cshtml extension. (C sharp HTML).
  • Razor syntax starts with @ symbol.

The example for a View is given below.

  1. Public class HomeController: Controller {  
  2.     Public ActionResult Index() {  
  3.         Viewbag.Countries = new list < string > () {“  
  4.             India”,  
  5.             “Australia”,  
  6.             “Japan”,  
  7.             “England”  
  8.         };  
  9.         Return View();  
  10.     }  
  11. }  
.CSHTML PAGE (View)
  1. @ {  
  2.     ViewBag.Title = ”Countries”  
  3. } < h2 > Countries list < h2 > < ul >  
  4.     // Note:- to switch between c sharp and html code we use @ symbol in view   
  5.     @foreach(string strCountries in Viewbag.Countries) { < li > @strCountries < /li>   
  6.     } < /ul>  
The code given above will print the list of countries in a View.

 

Ebook Download
View all
Learn
View all