ASP.NET MVC Models

ASP.NET MVC Models with Examples

In this article, I will explore ASP.NET MVC Models with relevant examples. Before diving into this topic, I recommend reviewing our previous article, where we covered ASP.NET MVC Views with examples. In this discussion, we will cover the following key points related to MVC Models.

1.What are the Models in ASP.NET MVC?
2.How to create and use Models in MVC.


What are the Models in ASP.NET MVC?

In an ASP.NET MVC application, Models are components consisting of a set of classes that represent business data (or domain data) and contain logic to manage that data. Simply put, the Model in ASP.NET MVC is responsible for handling domain data, representing the application's state in memory.
Note: While it is not compulsory, following best programming practices, it is advisable to store all model classes within the Models folder in an ASP.NET MVC application.

Example to Understand Models in ASP.NET MVC.

We need to display the employee information on a webpage.


To store the employee data, we will utilize the Employee model class. To create this class, right-click on the "Models" folder and choose the Add => Class option. Name the class Employee.cs and click the Add button to create the class, as illustrated in the image below.


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 Framework also contain 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 into 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 = "Arshiya",
                Gender = "Female",
                City = "Mumbai",
                Salary = 1000,
                Address = "Aurangabad"
            };
            return employee;
        }
    }
}                                       

After creating the necessary models for your application, the structure of the Models folder should appear as shown below.



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 all for now. In the next article, we will cover how to pass the employee model data to a view, enabling the view to generate the required HTML.


In the upcoming article, I will explain how to pass data from a controller to a view using ViewData in an ASP.NET MVC application. In this article, I aimed to provide a detailed explanation of ASP.NET MVC Models with examples. I hope this article serves your needs. I would love to hear your feedback. Please feel free to share any questions, comments, or feedback regarding this article.