Models in ASP.NET MVC

Models in ASP.NET MVC Application

In this article, I am going to discuss the Models in ASP.NET MVC application with an example. Please read our previous article before proceeding to this article where we discussed Views in ASP.NET MVC application. 

What are the Models in ASP.NET MVC?

The Models in ASP.NET MVC application are the component which contains a set of classes that are used to represent the business data as well as it also contains logic to manage the business data. So in the simple word we can say that the model in ASP.NET MVC is used to manage the domain data i.e. the state of the application in memory.

Note: It is not mandatory, but it is a good programming practice to store all model classes within the Models folder of MVC application.

Let us see an example to understand the Models in ASP.NET MVC.

We need to display the employee information in a webpage as shown below.

Models in ASP.NET MVC

In order to store the employee data, we are going to use the Employee model class. To do so,  Right click on the “Models” folder and then select Add => Class option. Provide the name as Employee.cs and finally click on the Add button as shown in the image below.

Adding Employee Models in ASP.NET MVC Application

Now open the Employee.cs class file and then copy and paste the following code.

namespace FirstMVCDemo.Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Gender { get; set; }
        public decimal Salary { get; set; }
    }
}

This is our Employee model which is going to hold the employee data in memory. As we already discussed, the Models in ASP.NET MVC also contains the business logic to manage the business data. So in our example, in order to manage the employee data i.e. to perform the CRUD operation on the employee data, we are going to use the following EmployeeBusinessLayer model.

Creating EmployeeBusinessLayer Model:

Right click on the Models folder and then add a new class file with the name EmployeeBusinessLayer.cs. Once you create the EmployeeBusinessLayer class file, then copy and paste the following code in it.

namespace FirstMVCDemo.Models
{
    public class EmployeeBusinessLayer
    {
        public Employee GetEmployeeDetails(int EmployeeId)
        {
            //Here we hardcoded the data
            //later we will discuss how to retrieve
            //the data from a database

            Employee employee = new Employee()
            {
                EmployeeId = EmployeeId,
                Name = "Pranaya",
                Gender = "Male",
                City = "Mumbai",
                Salary = 1000,
                Address = "Andheri"
            };

            return employee;
        }
    }
}

Once you created the required models for our application, then the model folder structure should look like below.Models in MVC

Modifying the HomeController

Now let us modify the HomeController class as shown below to use the Employee and EmployeeBusinessLayer model to retrieve the employee data.

using FirstMVCDemo.Models;
using System.Web.Mvc;
namespace FirstMVCDemo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index(int id)
        {
            EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer();
            Employee employee = employeeBL.GetEmployeeDetails(id);
            return View();
        }
    }
}

That’s it. In the next article, we will discuss how to pass the employee model data to a view, so that the view generates the necessary HTML to render the above employee model data.

In the next article, I will discuss how to pass the data from a controller to view using ViewData in ASP.NET MVC application.

SUMMARY

In this article, I try to explain the Models in ASP.NET MVC application. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *