Tuesday, February 9, 2016

What are Strongly Typed Views in MVC



A Strongly Typed view means it has a ViewModel associated to it that the controller is passing to it and all the elements in that View can use those ViewModel properties You can have strongly typed partials as well. Meaning that piece of Html needs specific data so you type it to a certain ViewModel.

 Here is an example of a Strongly Typed View @model SomeViewModel ...// all the html comes after A view that is strongly typed have a @model SomeViewModel line

 Here's one that renders a strongly typed View 

public ActionResult Index()
 {
   var model = new SomeViewModel();
    return View(model); 

And the view makes use of that ViewModel by having the @model SomeViewModel at the top of the file. So now that the view has a ViewModel I can display elements that are bound to the ViewModel like

@Model.Name

@Model.Location

OR for Controls------ @Html.TextBoxFor(m => m.Name) @Html.TextBoxFor(m => m.Location)

No comments:

Post a Comment