Strongly Typed Views

A strongly typed view is one that is designed to work with an object from your model (or view model, as discussed later in this chapter). You create a strongly typed view by adding the @model definition at the top of your view page. For example, you would add the following line to the top of view that works with a Customer instance.

@model AspNet5Unleashed.Models.Customer

This definition at the top of your page indicates to the MVC framework that the view expects a Customer object when the controller creates it. The controller passes this model data to the view and the runtime will assign this object to the view’s @model definition. It will also post this same customer object back to your POST action method on the controller when a user submits the form.

A strongly typed view allows you to use the model inside your markup (and with the @Html helper classes). For example, the following markup shows creating a label, text box, and validation message for the Name field from a model.

<div class="editor-label">
  @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
  @Html.EditorFor(model => model.Name)
  @Html.ValidationMessageFor(model => model.Name)
</div>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset