Html Label Helper in MVC 3 Razor

  • Html.Label is the helper used to render Label in MVC3 Razor.
  • Helpers are not controls by itself, they simply generate html markup.
Syntax to render Label :

UI :
On UI it is rendered as above.


Rendered HTML :



The Label helper takes expression as first parameter. The value provided for this parameter is used to set for attribute of label and also rendered as Label text if Label Text i.e. second parameter is not provided.


Overload For Label :


This overload accepts two parameters.

UI :

Rendered HTML :


This overload accepts two parameters. The first parameter is expression which becomes the for attribute and  the second parameter is the LabelText which becomes the text to be displayed on UI.


LabelFor and Model Binding :

This Helper accepts an expression, which binds label to the model.

UI :
The Helper uses the display property of model property User Name. This text is used as Label Text, when Label text is not specified explicitly.

Rendered HTML :



Overload For LabelFor :


This overload of LabelFor accepts two parameter. The first parameter is the expression which binds it to the model. The second parameter is the Label text.

UI :

Rendered HTML :


The first parameter becomes the for attribute and second parameter becomes the Label text.










0 comments:

Post a Comment