What are HTML Helpers in ASP.NET MVC?
HTML Helpers are nothing but the way of rendering HTML on the view page in ASP.NET MVC. Typically what we have in traditional ASP.NET web forms is Web Controls to achieve same functionality but with the evolution of MVC pattern , you don't have any web controls to add on to the view pages.
In simple words - it is just a method which returns you a string , and string is HTML.
Example of HTML Helpers:
ASP.NET MVC framework ships with various inbuilt HTML helpers such as ActionLink , Label.
Lets take a look at how a HTML helper is added to the view page. I am considering Razor view engine for this example.
HTML Helpers are nothing but the way of rendering HTML on the view page in ASP.NET MVC. Typically what we have in traditional ASP.NET web forms is Web Controls to achieve same functionality but with the evolution of MVC pattern , you don't have any web controls to add on to the view pages.
In simple words - it is just a method which returns you a string , and string is HTML.
Example of HTML Helpers:
ASP.NET MVC framework ships with various inbuilt HTML helpers such as ActionLink , Label.
Lets take a look at how a HTML helper is added to the view page. I am considering Razor view engine for this example.
@Html.ActionLink("Display Name of Link", "MethodName", "ControllerName");
Example above shows how OOB Action Link is used to render the HTML hyperlink on the view page.
Another example is shown as below where it is used to render the Html label tag to render Store Name property of model class.
@Html.LabelFor(model =>
model.StoreName)
Why to use Html Helpers?
Question might have poped up in your mind by now that Html helpers are just going to render the string of html then why do I need to use them ? If I know the Html syntax then why not add the Html directly on to the view page?
Answer is simple, it depends how clean and consistent you want to make your application. of course you can do the direct addition of html tags on your view pages but if you observe second example - you can see that it is simply rendering the label tag on view page for a property in model at run time. so it just for making developer's life more easy and to have the cleaner html for your view page.
Need of Custom Html Helpers?
Well, there is always need to do some custom stuff on top of what framework offers, and reason for doing this is either business requirement or to get things done in smarter way.
Writing a custom Html helper is nothing but writing an extension method. you are actually writing an extension method for HtmlHelper class.
Being a SharePoint developer I always find this task similar to creation of a web control where you override the render method of base class and do the custom Html rendering.
Creating Custom Html Helper
All right , for demo purpose I will keep the example simple - I will simply write an Html helper which will render the header of any content on view page.
This is done simply by using the <header> tag of Html 5.
Step 1: Define your custom static class for creation of your custom Html helpers
public static class DemoCustomHtmlHelpers
{
}
Step 2: Add static method to the class and make sure that return type is MvcHtmlString. Write an input paramter of method as HtmlHelper and also a string for which the header tag needs to be generated.
You might need to add System.Web.Mvc namespace to your class.
public static MvcHtmlString Header(this HtmlHelper helper, string content)
{
}
Step 3 : Add the rendering logic in the method , you can take help from TagBuilder class to generate Html tags to be rendered.
public static MvcHtmlString Header(this HtmlHelper helper, string content)
{
var tagBuilder = new TagBuilder("header");
tagBuilder.InnerHtml = content;
return new MvcHtmlString(tagBuilder.ToString());
}
Step 4: Build the project and open any of the view page. Now you can use your custom Html helper to render the header. Make sure that your view page has correct using namespace for using your custom Html helper extension.
@Html.Header("Create")
When you observe the view page's Html , you will be finding the generated Html tag by our Custom Html helper extension
<header>Create</header>
So this is all guys , have fun and try to create these Html Helper extensions in your scenarios.
Hi could you please answer some questions on this thread of your blog. http://passionatetechie.blogspot.com/2012/10/creating-custom-workflow-activity-for.html?showComment=1366984807757#c4627411411417251814
ReplyDeleteIt would be highly appreciated! Thanks.....
Perfect, thanks!
ReplyDeleteTo make your custom helpers available in all views add your namespace to the web.config inside the "Views" folder.