What is HTML Helper Class

HTML helpers are the way by which we can render html on the view page of MVC. These Helpers are simple functions that let the developer specify the type of HTML needed on the view.
The following built-in Helpers are available.
  1. Html.ActionLink
  2. Html.BeginForm
  3. Html.CheckBox
  4. Html.DropDownList
  5. Html.EndForm
  6. Html.Hidden
  7. Html.ListBox
  8. Html.Password
  9. Html.RadioButton
  10. Html.TextArea
  11. Html.TextBox
There are some Html Helper also available for strongly typed View.
  1. Html.CheckBoxFor
  2. Html.DropDownListFor
  3. Html.HiddenFor
  4. Html.ListBoxFor
  5. Html.PasswordFor
  6. Html.RadioButtonFor
  7. Html.TextAreaFor
  8. Html.TextBoxFor
The above list of HTML Helper is for strongly typed view. Inside these helper methods we pass lambda expression as a method argument.
But if we want to create our own HTML Helper Method then there are three ways to create your custom Html Helpers.
  1. Using Static Method
  2. Using Extension Method
  3. Using the @helper
Custom Helper Using Static Method
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace CustomHelper.CustomHelpers
  6. {
  7. /// <summary>
  8. /// This Class Helpes
  9. /// </summary>
  10. public static class HelperByStaticMethod
  11. {
  12. /// <summary>
  13. /// This is a Helper Method For Image Element
  14. /// </summary>
  15. /// <param name="Source">Provide Source</param>
  16. /// <param name="Alt">alt Attribute For That Image</param>
  17. /// <param name="Title">Title For the Image</param>
  18. /// <param name="Height">Height Attribute (By Default Height will be 128)</param>
  19. /// <param name="Width">Width Attribute (By Default Width will be 128)</param>
  20. /// <returns></returns>
  21. public static string img(string Source, string Alt, string Title, int Height=128, int Width=128)
  22. {
  23. return String.Format("<img src='{0}' alt='{1}' title='{2}' height='{3}' width='{4}'/>", Source, Alt, Title, Height, Width);
  24. }
  25. }
  26. }
And inside View we will use like the following:
  1. @using CustomHelper.CustomHelpers
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta name="viewport" content="width=device-width" />
  6. <title>Index</title>
  7. </head>
  8. <body>
  9. <div>
  10. @Html.Raw(HelperByStaticMethod.img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg"))
  11. @Html.Raw(HelperByStaticMethod.img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167))
  12. @Html.Raw(HelperByStaticMethod.img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250))
  13. @Html.Raw(HelperByStaticMethod.img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200))
  14. </div>
  15. </body>
  16. </html>
Output
In the above code I have created a static class and method taking Image Source, Alt, Title, Height and Width and returning simple string. But one problem is that I am explicitly converting these into html format using Html.Raw. If I don't convert this string into html format and keep my code as in the following,
  1. @HelperByStaticMethod.img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg")
  2. @HelperByStaticMethod.img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167)
  3. @HelperByStaticMethod.img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250)
  4. @HelperByStaticMethod.img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200)
then our output will be the following.
Custom Helper Using Extension Method
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace CustomHelper.CustomHelpers
  7. {
  8. public static class HelperByExt
  9. {
  10. public static MvcHtmlString img(this HtmlHelper htmlHelper,string Source, string alt, string title, int Height=128, int Width=128)
  11. {
  12. var imgTag = new TagBuilder("image");
  13. imgTag.MergeAttribute("src", Source);
  14. imgTag.MergeAttribute("alt", alt);
  15. imgTag.MergeAttribute("title", title);
  16. imgTag.MergeAttribute("width", Width.ToString());
  17. imgTag.MergeAttribute("height", Height.ToString());
  18. return MvcHtmlString.Create(imgTag.ToString(TagRenderMode.SelfClosing));
  19. }
  20. }
  21. }
In view we will use our Custom Helper class as in the following:

I am writing the following code for our view.
  1. @using CustomHelper.CustomHelpers
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta name="viewport" content="width=device-width" />
  6. <title>Index</title>
  7. </head>
  8. <body>
  9. <div>
  10. @Html.img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg")
  11. @Html.img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167)
  12. @Html.img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250)
  13. @Html.img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200)
  14. </div>
  15. </body>
  16. </html>
Output
Custom Helper Using @helper

This method is specific for only razor view engine. The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code. Let’s look at a super-simple scenario of how the @helper syntax can be used.

Because as I said @helper is specific for only razor view engine so we can use it only on view or cshtml file.

How to use @helper

I am writing the following code for our view.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width" />
  5. <title>Index</title>
  6. </head>
  7. <body>
  8. <div>
  9. @helper img(string Source, string alt, string title,
  10. int Height = 128, int Width = 128)
  11. {
  12. <img src="@Source" alt="@alt" title="@title" height="@Height" width="@Width"/>
  13. }
  14. @img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg")
  15. @img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167)
  16. @img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250)
  17. @img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200)
  18. </div>
  19. </body>
  20. </html>
Output

Finally, I am combining all view's code, which I have created for all type of ways to create HTML Helper that is as follows,
  1. @using CustomHelper.CustomHelpers
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta name="viewport" content="width=device-width" />
  6. <title>Index</title>
  7. </head>
  8. <body>
  9. <div>
  10. <h1>Helper By Static Method</h1>
  11. @Html.Raw(HelperByStaticMethod.img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg"))
  12. @Html.Raw(HelperByStaticMethod.img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167))
  13. @Html.Raw(HelperByStaticMethod.img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250))
  14. @Html.Raw(HelperByStaticMethod.img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200))
  15. <hr />
  16. <h1>Helper By Extension Method</h1>
  17. @Html.img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg")
  18. @Html.img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167)
  19. @Html.img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250)
  20. @Html.img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200)
  21. <hr />
  22. <h1>Helper By @@helper </h1>
  23. @helper img(string Source, string alt, string title,
  24. int Height = 128, int Width = 128)
  25. {
  26. <img src="@Source" alt="@alt" title="@title" height="@Height" width="@Width"/>
  27. }
  28. @img("/Images/1.jpg", "Mark Zuck", "Mark Zuckerberg")
  29. @img("/Images/2.jpg", "Bill Gates", "Bill Gates", 250, 167)
  30. @img("/Images/3.jpg", "APJ Abdul Kalam", "APJ Abdul Kalam", 250)
  31. @img("/Images/4.jpg", "Steve Jobs", "Steve Jobs", 200, 200)
  32. </div>
  33. </body>
  34. </html>
Output

Read more articles on ASP.NET: