Yousuf Tafhim
Yousuf Tafhim

Reputation: 361

What is the difference between Html.Label and Html.Display?

What is the difference between Html.Label and Html.Display?

Upvotes: 15

Views: 25953

Answers (3)

Bala R
Bala R

Reputation: 108947

Html.Label() renders HTML markup <label /> that can be used for a model entity's attrubute.

For eg,

<%= Html.Label("Full Name", Model.FullName) %>

would render

 <label for="FullName">Full Name </label>

Html.Display() on the other hand renders HTML markup for entire entity based on specified templates. For eg. if you have a Person entity with multiple attributes, you define a template with markup as to how to render a Person and Html.Display() uses that template to render Person objects across views. Phil Haack has an excellent post on display templates.

Upvotes: 15

Brandon Morales
Brandon Morales

Reputation: 140

Html.Display is more dynamic it generates different HTML markup depending on the data type of the property that is being rendered, and according to whether the property is marked with certain attributes. The method renders markup according to the following rules:

If the property is typed as a primitive type (integer, string, and so on), the method renders a string that represents the property value.

If a property is annotated with a data type attribute, the attribute specifies the markup that is generated for the property. For example, if the property is marked with the EmailAddress attribute, the method generates markup that contains an HTML anchor that is configured with the mailto protocol, as in the following example:

<a href='mailto:[email protected]'>[email protected]</a>

If the object contains multiple properties, for each property the method generates a string that consists of markup for the property name and for the property value.

Html.Label just generates a label tag like Male

Upvotes: 0

John Farrell
John Farrell

Reputation: 24754

Label

Returns an HTML label element and the property name of the property that is represented by the specified expression.

Display

Returns HTML markup for each property in the object that is represented by a string expression.

Upvotes: 3

Related Questions