Create A Strongly Typed View List in MVC

Strongly typed view is a nice feature of  ASP.NET MVC. We can access model properties on that view using strongly typed view and we can bind views with any model.

Now let's create a Strongly Typed View List in MVC.

Step 1
: Create a ASP.NET MVC application by selecting Empty Template and add Core Reference for MVC.

 
Step 2: Right click on your Model Folder and add a new Class File called 'Student.cs'.


Step 3
: Open your Student.cs file and create a Student property like RollNo, FirstName, LastName.
  1. public class Student  
  2. {  
  3.     public int RollNo  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string StudFirstName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string StudLastName  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
Step 4: Right click on Controller and new Empty Controller from the Add Scaffold.


Step 5:
Give a name to Controller like "StudentController."



Step 6
:
Open 'StudentController' file and create a list of Student, then assign RollNo, Name etc.

Finally assign the Stud Object to ViewData.Model.
  1. public ActionResult Index()    
  2. {    
  3.     List<Student> stud = new List<Student>    
  4.     {    
  5.         new Student { RollNo=1, StudFirstName="Ankur", StudLastName="Mistry" },    
  6.         new Student { RollNo=2, StudFirstName="Rahul", StudLastName="Lad" }    
  7.     };    
  8.   
  9.     ViewData.Model = stud;    
  10.   
  11.     return View();    
  12. }   
Step 7: Now, right click on Index in Controller and click on Add View.



From the Add View dialog box, Select the ModelClass Student (WebApplication1.Models), Now from Template tab select 'List' as we are going to display model data in List and Click on Add.



It will create a Student View in View folder as per below screen. I have removed edit, delete, and create Link from here as we are just going to display the data.


Now run your application, it will display the Data of Students.


Summary

So there are two main benefits of strongly typed view here.

1. Intellisense support.

2. ViewModel will be automatically scaffolded.

So here in our example, when we create a View by selecting Template as List, our ViewModel is automatically scaffolded.
  1. <table class="table">    
  2.     <tr>    
  3.         <th>    
  4.             @Html.DisplayNameFor(model => model.RollNo)    
  5.         </th>    
  6.         <th>    
  7.             @Html.DisplayNameFor(model => model.StudFirstName)    
  8.         </th>    
  9.         <th>    
  10.             @Html.DisplayNameFor(model => model.StudLastName)    
  11.         </th>    
  12.         <th></th>    
  13.     </tr>    
  14.     
  15. @foreach (var item in Model) {    
  16.     <tr>    
  17.         <td>    
  18.             @Html.DisplayFor(modelItem => item.RollNo)    
  19.         </td>    
  20.         <td>    
  21.             @Html.DisplayFor(modelItem => item.StudFirstName)    
  22.         </td>    
  23.         <td>    
  24.             @Html.DisplayFor(modelItem => item.StudLastName)    
  25.         </td>    
  26.       <a href="#">@item.RollNo</a>    
  27.     </tr>    
  28. }    
  29.     
  30. </table>  
I hope you liked this article.
 
Read more articles on ASP.NET:

Similar Articles