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.
-
29:22
Coin Stories with Natalie Brunell
2 days agoSaifedean: Bitcoin Poised for Cycle Top? Corporate Treasuries, Wealth Concentration & Gold Standard
1512 -
10:34
Chubbyemu
10 days agoA Lawyer Couldn't Sleep For 9 Days. This Is What Happened To Her Colon.
1.02K8 -
20:55
Boho Beautiful Yoga
9 days ago20 Min Yoga For Full Body Detox & Complete Energetic Reset | Come Back Home ❤
2.36K2 -
45:36
FULL SEND PODCAST
11 hours agoBill Maher on His Dating Life, Crazy Party Stories, and Trump Meeting!
1.73K1 -
2:01:05
Inverted World Live
4 hours agoElon and the Technocracy Strike Back | Ep. 54
13.9K11 -
7:06
Colion Noir
4 hours agoMass Shootings or Gang Violence? The Lie That’s Fueling Gun Control
36.5K13 -
1:03:16
Man in America
10 hours agoAI Is Their God—And You’re the Sacrifice
40.1K5 -
LIVE
Price of Reason
9 hours agoMusk vs Trump Rift ESCALATES Into ALL OUT WAR! The Phoenician Scheme REVIEW! Ubisoft Still WOKE?
732 watching -
14:12
Clownfish TV
13 hours agoDisney vs. YouTube Lawsuit is Getting Crazy...
7.54K4 -
3:33
The Official Steve Harvey
10 hours ago $0.28 earnedMy First Paycheck in the 1990s | Motivational Talks With Steve Harvey
11.7K2