Strongly Typed View List Sample in MVC: Day 24

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…".

    add new controller in MVC

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

    add new class

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

    class employee

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

    add another controller in MVC

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

    mvc controller

  7. Name the controller as in the following:

    employee controller

  8. In the Index action result method:

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

    controller

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

    employee controller code

  10. 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.

    create index

  11. It will automatically create a view from the model.

    html code

  12. 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.

    html link

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

    employee index
    << Error Handling Method 7: Day 7 of 23


Similar Articles