HTML Helpers in MVC3 Razor

  • HTML helpers are the extension methods that generate html.
  • HTML helpers are not controls, they simply generate html markup.
  • HTML helpers are the methods you can invoke on the html property of a view.
Advantages:

1. Automatic Encoding: The HTML helpers encode the values before rendering.


2. htmlAttributes parameter: Every HTML helper in the MVC framework includes an htmlAttributes parameter in one of the overloaded version. We can pass an anonymously typed object to the htmlAttributes parameter of any HTML helper. This parameter is used to pass and set attributes value like style, and other control level attributes.


3. Easy to Use: The HTML helpers are easy to use.


4. Flexibility: We can create our own HTML helpers instead of using built in helpers.


Inside HTML Helpers:

Every Razor view inherits an Html property from its base class. The Html property is of type System.Web.Mvc.HtmlHelper<T>, where T is a generic type parameter representing the type of the model for the view (dynamic by default). The class provides a few instance methods you can invoke in a view. However the framework defines the majority of the helpers as extension methods.
                 You must know you are working with extension methods when the IntelliSense window shows the method name with a blue down arrow to the left. Extension methods are a wonderful approach to building HTML helpers for two reasons:

1. Extension methods in C# are available only when the namespace of the extension method is in scope. All of the MVC's extension methods for HtmlHelper live in the System.Web.Mvc.Html namespace. This namespace is in scope by default thanks to namespace entry in the Views/web.config file. We can remove this namespace if we do not wish to use the built in extension methods.


2. The phrase "built your own" brings us to the second benefit of having helpers as extension methods. We can build our own extension methods to replace or augment the built-in helpers.

You can visit the below link to know how to built your own helper.
link

Helpers:

  • Html.LabelHtml.Label is the helper used to render Label in MVC3 Razor. 
  • Html.CheckBoxHtml.CheckBox is the helper used to render CheckBox in MVC3 Razor.
  • Html.CheckBoxForHtml.CheckBoxFor is the helper used to render CheckBox in MVC3 Razor.
  • Html.PasswordHtml.Password is the helper which renders a Textbox.
  • Html.RadioButtonHtml.RadioButton is the helper used to render a RadioButton control.
  • Html.RadioButtonForHtml.RadioButtonFor is the helper used to render a RadioButton control.
  • Html.TextAreaHtml.TextArea is the helper which renders a TextArea.
  • Html.TextBoxHtml.TextBox is the helper which renders a TextBox.
  • Html.TextBoxForHtml.TextBoxFor is the helper which renders a TextBox.
  • Html.HiddenHtml.Hidden is the helper which renders a Hidden field.
  • Html.HiddenForHtml.HiddenFor is the helper which renders a Hidden field.
  • Html.ActionLinkThe ActionLink method renders a hyperlink (anchor tag) to another controller action.
  • Html.Action - The Action Url helper is exactly like ActionLink, but does not return an anchor tag.
  • Html.Content - The Content helper is used to specify absolute path.
  • Html.EditorForModelThis HTML Helper generates HTML Labels and editor elements for the properties in the model object.

0 comments:

Post a Comment