Click here to Skip to main content
12,253,554 members (61,744 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.8K views
6 bookmarked
Posted

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

, 22 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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 approach

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 = &ldquo;1&Prime;, EmpFirstName = &ldquo;Imran&rdquo;, EmpLastName = &ldquo;Ghani&rdquo;},
               new Employee{EmpID = &ldquo;2&Prime;, EmpFirstName = &ldquo;Rizwan&rdquo;, EmpLastName = &ldquo;Mukhtar&rdquo;},
               new Employee{EmpID = &ldquo;3&Prime;, EmpFirstName = &ldquo;Rehan&rdquo;, EmpLastName = &ldquo;Ahmad&rdquo;},
               new Employee{EmpID = &ldquo;4&Prime;, EmpFirstName = &ldquo;Zeeshan&rdquo;, EmpLastName = &ldquo;Khalid&rdquo;},
               new Employee{EmpID = &ldquo;5&Prime;, EmpFirstName = &ldquo;Sajid&rdquo;, EmpLastName = &ldquo;Majeed&rdquo;}
           };

           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=&rdquo;viewport&rdquo; content=&rdquo;width=device-width&rdquo; />
          <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)

Share

About the Author

Imran Abdul Ghani
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.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 Jun 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid