Strongly Typed View List Sample in MVC

Strongly Typed View List Sample in MVC

In this article we will display a list of employees by creating a strongly typed view.

  1. Create a MVC project from the "Empty" template.
  2. Right-click on "Models", select "Add" >> "Class…".

2.jpg

Name the class "Employee" and click on the "Add" button.

3.jpg

In the employee class create employee properties like EmployeeId, FirstName, LastName and so on.

4.jpg

Now add a controller; right-click on "Controllers", select "Add" >> "Controller…".

5.jpg

Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.

6.jpg

Name the controller as in the following:

7.jpg

In the Index action result method:

Create a list of employees with some dummy employee details Assign the list to ViewData.Model

8.jpg

Now add a view. Right-click on the "Index" action method and select "Add View…".

9.jpg

Name the view as "Index".

Since we will show a list of employees, select "List" from the template. From the model class drop down, select "Employee" as model. Click on the "Add" button.

10.jpg

It will automatically create a view from the model.

11.jpg

Remove the "Create New" link from the top and The "Edit", "Details" and "Delete" links from the bottom of the table since we only want to display a list of employees.

12.jpg

Now run the project and you can have a list of employees in the browser.

13.jpg