Understanding Of Models In An MVC Application

In this blog topic, we shall discuss the models in an MVC application.

Let’s understand the models with the help of examples. Suppose it’s an effort to retrieve employee information from the tblEmployee table and exhibit it.

asp dot net mvc

To envelope Employee information, Employee model class must be added to the Models folder. How to do this?

Steps:

1. Right click on “Models” folder > Add > Class

2. Write the name for 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; }

}

Next add EmployeeController class to “Controllers” folder.

Steps:

1. Right click the “Controllers” folder > Add > Controller

2. Use EmployeeController as name

3. Click “Add”

It is to use “Employee” model class EmployeeController. Next, copy and paste this “using” statement in “EmployeeController.cs”

using MVCDemo.Models;

An Index () Action method is created, by default in EmployeeController. Change the name of the function to Details(). Develop an instance of Employee class. Next, you can hard code Employee data in this class. Now EmployeeController should appear as shown below.

public ActionResult Details()

{

Employee employee = new Employee()

{

EmployeeId = 101,

Name = “John”,

Gender = “Male”,

City = “London”

};

return View();

}

Then you have to pass employee model object which was created in EmployeeController to the view, such that it can produce the HTML and send to the client. So you need to add a view.

Steps:

1. Right click on Details() action method and choose “Add View” from the context menu

2. Set

a)View Name = Details

b)View Engine = Razor

c)Select “Create strongly typed view” checkbox

d)From the “Model class” drop-down list, select “Employee (MVCDemo.Models)”

3. Next and finally, click “Add”

Here, Details.cshtml must be added to “Employee” folder. Take a note that “Employee” folder is created automatically and added to “Views” folder.

Copy and paste the below code into Details.cshtml file.

@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>

Now if you try running the project, and if you go to the following URL, you will get a runtime error saying: Object reference is not set to an instance of an object.

http://localhost/MVCDemo/Employee/Details

And the best way to correct this error is pass “Employee” object to the view. The “return” statement in Details() action method has to be modified as given below:

return View(employee);

Done!

Run the application and go to http://localhost/MVCDemo/Employee/Details.

With this, we conclude. Happy coding!

If you want to enhance yourself in Dot Net Course and improve yourself through Dot NET training program; our institute, CRBtech Solutions would be of great help and support. We give a well-structured program for the best Dot Net Course. Among many reputed institutes of dot net training and placement in Pune, CRB Tech has created a niche for itself.

Don't be shellfish...Buffer this pageEmail this to someoneDigg thisShare on FacebookShare on Google+Share on LinkedInPrint this pageShare on RedditPin on PinterestShare on StumbleUponTweet about this on TwitterShare on Tumblr

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>