Passing Data From Controller To View In ASP.NET MVC

In this article we see how to pass data from Controller to View in ASP.NET MVC.

Before reading this I would request you to go through previous articles on ASP.NET MVC:

I hope the above articles are clear by now. Let us start with the application. Open Visual Studio. Go to File->New->Project. Give a suitable name to the application. Click OK.



Select Empty Template. Click OK.



Now our application is created. We have a very basic MVC structure in the application with primarily three folders: Model, View and Controller.

Let us add a class in the Model folder. Right click on Model folder. Go to Add->Class. I have named it Student.cs.



Let us add few basic properties to our model now.

  1. public class Student  
  2. {  
  3.     publicintStudentID  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Name  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     publicint Marks  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string City  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  
Now we will add a Controller. Right click on Controller. Add->Controller.



Select MVC 5 Empty Controller. Click Add.



We will give a suitable name to the controller. Click Add once name is assigned.



Let us write some code in the controller class.
  1. usingMVCPassDatafromModel.Models;  
  2. using System;  
  3. usingSystem.Collections.Generic;  
  4. usingSystem.Linq;  
  5. usingSystem.Web;  
  6. usingSystem.Web.Mvc;  
  7. namespaceMVCPassDatafromModel.Controllers  
  8. {  
  9.     public class GetDetailsController: Controller  
  10.     {  
  11.         // GET: GetDetails  
  12.         publicActionResultGetStudents()  
  13.         {  
  14.             List < Student > Objstud = new List < Student > ()  
  15.             {  
  16.                 new Student  
  17.                 {  
  18.                     StudentID = 1, Name = "Nitin Tyagi", Marks = 90, City = "New Delhi"  
  19.                 },  
  20.                 new Student  
  21.                 {  
  22.                     StudentID = 2, Name = "AbhijeetSingh", Marks = 80, City = "Hyderabad"  
  23.                 },  
  24.                 new Student  
  25.                 {  
  26.                     StudentID = 3, Name = "Arjun Singh", Marks = 90, City = "Jaipur"  
  27.                 },  
  28.             };  
  29.             return View(Objstud);  
  30.         }  
  31.     }  
  32. }  
We have defined a List of Students and populated it with data. We would be passing this data to the View. Since we do not have a views we will create a View. Right click on GetStudents() method. Go to Add->View. Select the empty template. Click Add.



Write the following Razor code in GetStudents.cshtml.
  1. @modelIEnumerable < MVCPassDatafromModel.Models.Student >  
  2. @  
  3. {  
  4.     Layout = null;  
  5. }  
  6.   
  7. <!DOCTYPE html >  
  8. <html >  
  9.     <head >  
  10.         <meta name = "viewport"  content = "width=device-width" / >  
  11.         <title > GetStudents < /title>  
  12.       
  13.     </head>  
  14.     <body >  
  15.         <div >  
  16.             <h2 > Student List < /h2>  
  17.                 @foreach(vari in Model)  
  18.                 {   
  19.                     < p > @i.StudentID | @i.Name | @i.Marks | @i.City < /p>  
  20.                 }  
  21.         </div>  
  22.       
  23.     </body>  
  24.       
  25. </html>  
We have used a foreach loop to iterate the model that we have passed from the controller. For this purpose we have used the following markup to create a strongly typed view.

@modelIEnumerable<MVCPassDatafromModel.Models.Student>

Let us run the application now and see the output.



We have our data displayed in the view. This is how we can display data from controller to view.