An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. HTML helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state. With MVC, you can create your own helpers, or use the built in HTML helpers.


<%= Html.TextBox("LastName") %>

Following HTML helpers are built into the ASP.NET MVC framework:

* Html.TextBoxFor

* Html.TextAreaFor

* Html.PasswordFor

* Html.HiddenFor

* Html.CheckBoxFor

* Html.RadioButtonFor

* Html.DropDownListFor

* Html.ListBoxFor

Creating Custom HTML Helpers

The Custom HTML Helpers can be created in two ways as below

1. Creating Extension Method on Html Helper Class

2. Creating Static Methods


ASP.NET MVC Custom Html Helper using Extension Method

If you want to create HTML Helpers that work just like the standard HTML Helpers included in the ASP.NET MVC framework then you need to create extension methods. Extension methods enable you to add new methods to an existing class. When creating an HTML Helper method, you add new methods to the HtmlHelper class represented by a view's Html property.

After you create an extension method, and build your application successfully, the extension method appears in Visual Studio Intellisense like all of the other methods of a class (see Figure 2). The only difference is that extension methods appear with a special symbol next to them (an icon of a downward arrow).

Creating HTML Helpers with Static Methods

It’s also as simple as that of above mentioned Extension Method approach. We will create a static method for TextBox that renders an HTML TextBox as string.

0 Comments1

Sorry, No more post.

Your Comment

Login To Comment


By posting your Comment, you agree to the privacy policy and terms of service.