Model In ASP.NET MVC 5

We will learn models in MVC 5 and add model in MVC 5. Also, we will learn about passing the data from Controller to View in ASP.NET MVC 5 with examples.

Model In ASP.NET MVC 5
 
In this article, you will learn the following points about models in MVC 5.
  • What is the Model in MVC 5?
  • How to add Model in MVC 5?
  • How to pass data from Controller to View ?.
  • How to use the Model in View?
  • How to bind model List in View?
Following are the previews two articles on ASP.NET MVC 5.

What is the Model in MVC 5

  • In MVC M stands for Model and Model is a normal C# class.
  • Model is responsible for handling data and business logic.
  • A model represents the shape of the data.
  • Model is responsible for handling database related changes
Model diagram as follows.
 
Model In ASP.NET MVC 5
 
As per the above figure, user request enters the URL on the browser the given request go to the server and call the routing which will execute the appropriate controller. And, based on the request controller, execute the appropriate controller action method. It will pass the request to model if the model has a database related operation. Then, it will perform the database-related operation and send the request back to the controller. After completing this request, the controller returns the response to the user.
 

How to add Model in MVC 5?

 
Create a model name like 'Product.cs” inside the model folder.
 
Step 1
 
First, create an ASP.NET MVC application using Visual Studio 2017 and provide the name “MVC5ModelDemo”.
 
Model In ASP.NET MVC 5
 
Step 2
 
Go to solution explorer => Views Folder => Right-click on “Model” Folder >> go to “Add” >> Click on [Class] as follow.
 
Model In ASP.NET MVC 5
 
Step 3
 
Provide the required name like “Product.cs” then click on “Add” button as follow.
 
Model In ASP.NET MVC 5
 
We can add properties and method in Product class as per our requirement as follow.
  1. public class Product  
  2. {  
  3.     public int ProductId { getset; }  
  4.     public string ProductName { getset; }  
  5.     public int OrderId { getset; }  
  6.     public int Quantity { getset; }  
  7.   
  8.     public static Product GetProduct()  
  9.     {  
  10.         Product product = new Product() { ProductId = 01, ProductName = "C# Book", OrderId = 02, Quantity = 2 };  
  11.         return product;  
  12.     }  
  13. }  
In the above product model class, we have added required properties and “GetProduct()” method which will return the product.
 

How to pass data from Controller to View?

 
First, we need to import the model namespace on HomeController “using MVC5ModelDemo.Models”. Add the following code in HomeController as follow.
  1. public class HomeController : Controller  
  2. {  
  3.     // GET: Home  
  4.     public ActionResult Index()  
  5.     {  
  6.         Product product = Product.GetProduct();  
  7.         return View(product);  
  8.     }  
  9. }  
 In the above code, we have passed the product model to View.
 

How to use the Model in View?

 
Create “Index.cshtml” page in Home folder inside the View. To access the product model in view first, we need to import the Product model namespace then we can use the product model in view. Use the namespace “@model MVC5ModelDemo.Models.Product” on top of the view page and add the following code in “Index.cshtml” View.
  1. @model MVC5ModelDemo.Models.Product  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h3>Product Page</h3>  
  8. <table border="1" class="table table-bordered table-responsive">  
  9.     <thead>  
  10.         <tr class="btn-primary">  
  11.             <th>Product ID</th>  
  12.             <th>Product Name</th>  
  13.             <th>Order ID</th>  
  14.             <th>Quantity</th>  
  15.         </tr>  
  16.     </thead>  
  17.     <tbody>  
  18.         <tr>  
  19.             <td>@Model.ProductId</td>  
  20.             <td>@Model.ProductName</td>  
  21.             <td>@Model.OrderId</td>  
  22.             <td>@Model.Quantity</td>  
  23.         </tr>  
  24.     </tbody>  
  25. </table>  
After importing model namespace create the HTML table to display the product model detail. We can use @Model property to get the product model property as above.
 
Output
 
Model In ASP.NET MVC 5
 

How to bind model List in View

 
To bind the model list to view first we need to create a method which returns the list of product in Product Class as follow.
  1. public static List<Product> GetAllProduct()  
  2. {  
  3.     List<Product> products = new List<Product>() {  
  4.         new Product() { ProductId = 01, ProductName = "C# Book", OrderId = 02, Quantity = 2 },  
  5.         new Product() { ProductId = 02, ProductName = "ASP.NET Book", OrderId = 02, Quantity = 2 },  
  6.         new Product() { ProductId = 03, ProductName = "ASP.NET CORE Book", OrderId = 02, Quantity = 2 }  
  7.     };  
  8.               
  9.     return products;  
  10. }  
Call the “GetAllProduct()” method from the controller and pass the product list to the View as follow. Add following code in Home controller.
  1. public ActionResult Index()  
  2. {  
  3.     List<Product> productList = Product.GetAllProduct();  
  4.     return View(productList);  
  5. }  
Add the following code in Index Html page to bind the model list and display the result to the browser.
 
Use the foreach loop to retrieve the product model list data and display to the HTML table.
  1. @model IEnumerable<MVC5ModalDemo.Models.Product>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6. <h3>All Product List</h3>  
  7. <table border="1" class="table table-bordered table-responsive">  
  8.     <thead>  
  9.         <tr class="btn-primary">  
  10.             <th>Product ID</th>  
  11.             <th>Product Name</th>  
  12.             <th>Order ID</th>  
  13.             <th>Quantity</th>  
  14.         </tr>  
  15.     </thead>  
  16.     <tbody>  
  17.         @foreach (var item in Model)  
  18.         {  
  19.             <tr>  
  20.                 <td>@item.ProductId</td>  
  21.                 <td>@item.ProductName</td>  
  22.                 <td>@item.OrderId</td>  
  23.                 <td>@item.Quantity</td>  
  24.             </tr>  
  25.         }  
  26.     </tbody>  
  27. </table>  

Output

 
Model In ASP.NET MVC 5
 
References
I hope you understand the concepts of Model in ASP.NET MVC 5. If you like this article then please share this article that will help other people to increase their knowledge.
 
Thanks for reading.