LearnFast.Ninja

ASP.NET MVC how to pass model to view

In this tutorial you will learn how to pass model object instance of class form the controller to the view. If we work with the MVC design we want the controller and the view to work with the model object, so follow these steps and see how to pass model to view!

Step 1: Create Model class. In this example we created a User class.


Step 2: Create a Controller and then View for the Index Action.



Step 3: In the Controller use the Model library.
using MyMVC.Models;

Step 4: Create instance of User and pass it to the view.

public ActionResult Index()

{

    User William = new User("William""Smith");

    return View(William);

}


Step 5: At the top of the view (cshtml file) write @model ProfectName.Models.ClassName.

@model MyMVC.Models.User



Result:


Tags

asp.net

mvc model view