Loading

ASP.NET MVC

How to create Models in ASP.NET MVC Application?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

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

Let us see an example to understand the 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.

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 = "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.

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();
}
}
}

Thats it. In the next Video, we will discuss how to pass the employee model data to a view, so that the view generates the necessary HTML.

See All

Comments (266 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /266

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /266

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /266