January 22, 2019

Srikaanth

How To Create Models In ASP.NET MVC Applications

In this Page, you will learn about the Model in ASP.NET MVC framework.

Model represents domain specific data and business logic in MVC architecture. It maintains the data of the application. Model objects retrieve and store model state in the persistence store like a database.

Model class holds data in public properties. All the Model classes reside in the Model folder in MVC folder structure.

Let's see how to add model class in ASP.NET MVC.

How To Create Models In ASP.NET MVC Applications
How To Create Models In ASP.NET MVC Applications

To encapsulate Employee information, add Employee model class to the Models folder. To do this

1. Right click on "Models" folder > Add > Class

2. Name the class as Employee.cs

3. Click "Add"

Copy and paste the following code in Employee.cs class file.
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
}

Now let's Add EmployeeController class to "Controllers" folder. To do this

1. Right click on "Controllers" folder > Add > Controller

2. Use EmployeeController as the name

3. Click "Add"

We want to use "Employee" model class in EmployeeController. So copy and paste the following "using" statement in "EmployeeController.cs"
using MVCDemo.Models;



Subscribe to get more Posts :