Click here to Skip to main content
15,890,882 members
Articles / All Topics

Using Model to pass data to View in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Jun 2014CPOL2 min read 37.3K   6  
Exploring multiple ways to pass data from Controller to View in an ASP.NET MVC application, so far we have covered following different approaches: Part 1 – Using ViewData & ViewBag Part 2 – Using TempData | TempData Vs Session Now in this part, we are going to implement another appr

Exploring multiple ways to pass data from Controller to View in an ASP.NET MVC application, so far we have covered following different approaches:

Now in this part, we are going to implement another approach i.e. using Model for passing data from ASP.NET MVC Controller to View. As we already know that in MVC (Model, View, Controller) pattern, Model represents our domain model corresponding to tables in database. So, we can use this model class to serve the purpose.

Practical Guide to ASP.NET Web API

Consider the following Model class we have already used in our previous implementations:

namespace MyMVCApp.Models
{
           public class Employee
          {
                      public string EmpID { get; set; }
                      public string EmpFirstName { get; set; }
                      public string EmpLastName { get; set; }
           }
}

Now, we are done with our Model class (i.e. Employee). For the purpose of this implementation, we are loading data directly to our Model in EmployeeController. In a real implementation, we must be fetching this data from a data source e.g. a database. So, the Controller has following code:

public class EmployeeController : Controller
{
      public ActionResult Index()
      {
           List<Employee> employees = new List<Employee>()
           {
               new Employee{EmpID = "1″, EmpFirstName = "Imran", EmpLastName = "Ghani"},
               new Employee{EmpID = "2″, EmpFirstName = "Rizwan", EmpLastName = "Mukhtar"},
               new Employee{EmpID = "3″, EmpFirstName = "Rehan", EmpLastName = "Ahmad"},
               new Employee{EmpID = "4″, EmpFirstName = "Zeeshan", EmpLastName = "Khalid"},
               new Employee{EmpID = "5″, EmpFirstName = "Sajid", EmpLastName = "Majeed"}
           };

           return View(employees);
      }
}

In above code example, you can see that as opposite to ViewData and ViewBag approach in other ASP.NET MVC Tutorial, we are passing data (i.e. employees) as parameter to View instead of placing in another store and calling as View(). Finally, updating our View to get and display data passed from Controller as:

@model IEnumerable<MyMVCApp.Models.Employee>

<html>
<head>
          <meta name="viewport" content="width=device-width" />
          <title>Employee Index Page</title>
</head>
<body>
   <div>
       <h1>Employee (Using Model)</h1>
       <div>
         @foreach(var employee in Model){
                 <p>@employee.EmpID  @employee.EmpFirstName  @employee.EmpLastName</p>
         }
       </div>
   </div>
</body>
</html>

At the top of our View (i.e. Index.cshtml), we have referred to our Model class (i.e. Employee) using IEnumerable. Further in View, we are using foreach to access and display data as needed. When we run this application, our View will display the Model data from Controller as follows:

Model in ASP.NET MVC

Using Model to pass data to View in ASP.NET is pretty simple as well as more appropriate because we are fetching data from permanent store and loading in our Model classes in most of the cases. Also, we use it to perform all CRUD (Create, Retrieve, Update, Delete) operations. Hopefully, this ASP.NET MVC Tutorial will help in understanding the mentioned approach in a more effective way. Previous: ViewBag and ViewData in ASP.NET MVC

Other Related Articles:

The post Using Model to pass data to View in ASP.NET MVC appeared first on Web Development Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
-- There are no messages in this forum --