Introduction
In this amazing blog, you will get an introduction about the model class in the ASP.NET MVC framework.
A model is defined as a collection of classes that contains the business logic of the application(models are business domain-specific containers). Model is also used for accessing data from the database. The model class does not handle direct input from the browser.
What is a Model?
Models are also referred to as objects used to implement conceptual logic for the application. The model represents all the data in your application. It can be a table that you are storing inside SQL Server, or it can be a model, which will be a combination of multiple tables, and so on. A controller interacts with the model, accesses the data, performs the logic, and passes that data to the view.
In the ASP.NET MVC(Model-View-Controller) application, all the Model classes must be created in the Model folder.
Create a Model class
Let's understand model with an example:
If i want to retrieve employee information from this table TBL employee and then display that specific employee information in our MVC application, as you can see on the slide.
To encapsulate employee information, I need to have an employee model class. So, let's go ahead and add an employee model class to our MVC project.
Add the Employee model class to the Models folder to encapsulate Employee information.
To do this:
1. Right-click on "Models" folder > Add > Class
2. Name the class as Employee.cs
3. Click "Add".
Employee.cs code:
using System;
using System.collections.Generic;
using System.Linq;
using System.Web;
namespace MVCDemo.Models {
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 the "Controllers" folder.
To do this:
1. Right-click on "Controllers" folder > Add > Controller
2. Use EmployeeController as the name
3. Click "Add."
EmployeeController.cs code:
using System;
using System.collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;
namespace MVCDemo.Models {
public ActionResult Details() {
Employee employee = new Employee() {
EmployeeId = 101,
Name = "John",
Gender = "Male",
City = "London"
};
return View();
}
}
So, now we need to pass the employee model object that we constructed in EmployeeController to a view, so the view can generate the HTML and send it to the requested client. To do this step, we first need to add a view.
To add a view:
1. Right-click on Details() action method and select "Add View" from the context menu.
2. Set
a)View Name = Details
b)View Engine = Razor
c)Select "Create strongly-typed view" check box
d)From the "Model class" drop-down list, select "Employee (MVCDemo.Models)".
3. Finally, click "Add."
Details.cshtml code:
@model MVCDemo.Models.Employee
@{
ViewBag.Title = "Employee Details";
}
<h2>Employee Details</h2>
<table style="font-family:Arial">
<tr>
<td>Employee ID:</td>
<td>@Model.EmployeeId</td>
</tr>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender:</td>
<td>@Model.Gender</td>
</tr>
<tr>
<td>City:</td>
<td>@Model.City</td>
</tr>
</table>
Output: