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
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.Label - Html.Label is the helper used to render Label in MVC3 Razor.
- Html.CheckBox - Html.CheckBox is the helper used to render CheckBox in MVC3 Razor.
- Html.CheckBoxFor - Html.CheckBoxFor is the helper used to render CheckBox in MVC3 Razor.
- Html.Password - Html.Password is the helper which renders a Textbox.
- Html.PasswordFor - Html.PasswordFor is the helper which renders a Textbox.
- Html.RadioButton - Html.RadioButton is the helper used to render a RadioButton control.
- Html.RadioButtonFor - Html.RadioButtonFor is the helper used to render a RadioButton control.
- Html.TextArea - Html.TextArea is the helper which renders a TextArea.
- Html.TextAreaFor - Html.TextAreaFor is the helper which renders a TextArea.
- Html.TextBox - Html.TextBox is the helper which renders a TextBox.
- Html.TextBoxFor - Html.TextBoxFor is the helper which renders a TextBox.
- Html.Hidden - Html.Hidden is the helper which renders a Hidden field.
- Html.HiddenFor - Html.HiddenFor is the helper which renders a Hidden field.
- Html.DropDownList - Html.DropDownList renders a dropdown list.
- Html.DropDownListFor - Html.DropDownList renders a dropdown list.
- Html.ActionLink - The 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.ValidationMessage - Html.ValidationMessage helper is used to display validation errors or messages.
- Html.ValidationSummary - The ValidationSummary HTML Helper is used to display error messages.
- Html.EditorForModel - This HTML Helper generates HTML Labels and editor elements for the properties in the model object.
0 comments:
Post a Comment