ASP.NET MVC Models

ASP.NET MVC Models with Examples

In this article, I am going to discuss ASP.NET MVC Models with Examples. Please read our previous article before proceeding to this article where we discussed ASP.NET MVC Views with Examples. As part of this article, we are going to discuss the following pointers which are 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?

The Models in ASP.NET MVC application are the component that contains a set of classes that are used to represent the business data (or domain data) as well as logic to manage the business data. So in simple words, 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 an ASP.NET MVC application.

Example to Understand Models in ASP.NET MVC.

We need to display the employee information on 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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; }
}
}
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; } } }
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
}
}
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; } } }
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 your 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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();
}
}
}
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(); } } }
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.

In the next article, I am going to discuss how to pass the data from a controller to view using ViewData in the ASP.NET MVC application. Here, in this article, I try to explain ASP.NET MVC Models with Examples. 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.

5 thoughts on “ASP.NET MVC Models”

  1. blank

    am unable to create as you created in EmployeeBusinesslayer by its name
    so i have created employee object and using the same object selecting city, address, etc…
    please suggest
    public Employee GetEmployeeDetails(int EmployeeID)
    {
    Employee employee = new Employee();
    {
    employee.EmployeeId = EmployeeID;
    Name = “Amrutha”; // i can’t fetch
    employee.Address = “KS Layout”;
    employee.City = “Banglore”;
    employee.Gender = “Female”;
    employee.Salary = 10000;
    };
    return employee;

  2. blank

    Please remove semicolon where Employee is being instantiated ie
    public Employee GetEmployeeDetails(int EmployeeID)
    {
    Employee employee = new Employee() //no semicolon here
    {
    employee.EmployeeId = EmployeeID;
    Name = “Amrutha”; // i can’t fetch
    employee.Address = “KS Layout”;
    employee.City = “Banglore”;
    employee.Gender = “Female”;
    employee.Salary = 10000;
    };
    return employee;

    1. blank
      Rajkumar Prajapati

      public Employee GetEmployeeDetails(int EmployeeID)
      {
      Employee employee = new Employee() //no semicolon here
      {
      EmployeeId = EmployeeID, // wrong employee.EmployeeId = EmployeeID;
      Name = “Amrutha”,
      Address = “KS Layout”,
      City = “Banglore”,
      Gender = “Female”;
      Salary = 10000
      };
      return employee;

Leave a Reply

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