
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?
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.

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”.

Step 2
Go to solution explorer => Views Folder => Right-click on “Model” Folder >> go to “Add” >> Click on [Class] as follow.

Step 3
Provide the required name like “Product.cs” then click on “Add” button as follow.

We can add properties and method in Product class as per our requirement as follow.
- public class Product
- {
- public int ProductId { get; set; }
- public string ProductName { get; set; }
- public int OrderId { get; set; }
- public int Quantity { get; set; }
- public static Product GetProduct()
- {
- Product product = new Product() { ProductId = 01, ProductName = "C# Book", OrderId = 02, Quantity = 2 };
- return product;
- }
- }
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.
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- Product product = Product.GetProduct();
- return View(product);
- }
- }
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.



Prashant RewatkarPosted Sep 11, 2019, 12:36 PM
Explained step by step, Nice article @@