Premium Only Content

Part 7 Models in an mvc application
In this video we will discuss models in an mvc application.
Let's understand models with an example. We want to retrieve an employee information from tblEmployee table and display it in a view.
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;
By default an Index() Action method is created in EmployeeController. Change the name of the function to Details(). Create an instance of Employee class. For now we will hard code Employee data in this class. In a later video session, we will discuss about retrieving employee information from the database table tblEmployee. At this point EmployeeController should look as shown below.
public ActionResult Details()
{
Employee employee = new Employee()
{
EmployeeId = 101,
Name = "John",
Gender = "Male",
City = "London"
};
return View();
}
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 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" dropdownlist, select "Employee (MVCDemo.Models)"
Note: If Employee class is not visible in the dropdownlist, please build your project and then try adding the view again.
3. Finally click "Add"
At this point, Details.cshtml should be added to "Employee" folder. Please note that "Employee" folder is automatically created and added to "Views" folder.
At this point if you run the project, and if you navigate to the following URL, you get a runtime error stating - Object reference not set to an instance of an object.
localhost/MVCDemo/Employee/Details
To fix this error, pass "Employee" object to the view. The "return" statement in Details() action method need to be modified as shown below.
return View(employee);
That's it. Run the application and navigate to localhost/MVCDemo/Employee/Details. We should get the output as expected.
-
5:41:58
MattMorseTV
11 hours ago $112.25 earned🔴Antifa RIOT vs. Federal OFFICERS.🔴
188K219 -
1:05:28
Man in America
13 hours agoLIVE: Digital ID & the DEATH of Freedom—An URGENT Warning
64.3K74 -
40:24
The Connect: With Johnny Mitchell
1 day ago $6.52 earnedInside The WORST Drug-Infested Slums Of Medellin, Colombia
22.9K9 -
1:43:56
Tundra Tactical
10 hours ago $34.11 earned🛑LIVE NOW!! FBI Gets Caught LYING About Good Guys With Guns For 10 YEARS!!!!
53.2K4 -
2:12:01
BlackDiamondGunsandGear
2 days agoAFTER HOURS ARMORY / Antifa / Lies/ Prison time
31.5K1 -
2:12:00
DLDAfterDark
9 hours ago $13.62 earnedThe After Hours Armory! Tonight is The Chat's Chat! God, Guns, and Gear!
42.6K4 -
3:32:18
Mally_Mouse
12 hours ago🌶️ 🥵Spicy BITE Saturday!! 🥵🌶️- Let's Play: Phasmophobia
52K5 -
1:13:19
iCkEdMeL
8 hours ago $10.16 earnedChaos Explodes in Chicago & Portland | Feds Clash with Protesters!
52.4K29 -
21:54
Exploring With Nug
1 day ago $10.53 earnedScuba Diving Missing Person Search Leads to Discovery of Classic Cars!
66.8K19 -
8:06:32
Phyxicx
13 hours agoStar Wars: Movie Battles II Community Event hosted by ReaperAF95 - 10/4/2025
31.5K