Inline and Custom HTML Helpers In ASP.NET MVC

With the help of the HTML helper class, we can create HTML controls programmatically. HTML helpers are used in view to render HTML content. HTML helper is a method which helps to return a string.
 
HTML Helpers are categorized into three types:
  1. Inline HTML helpers
  2. Built-in HTML helpers
  3. Custom HTML helpers
In this article, we will learn how to create inline and custom HTML helpers. We have already covered built-in HTML helpers in the previous articles of this series. In previous ASP.NET MVC tutorials of this series, we saw,
Inline HTML Helpers

@helper syntax is used to create reusable inline helper method. They make the code more reusable, as well as more readable.
 
Let’s see how to use them.

Create an empty ASP.Net MVC Application.



Right click the controller and add a controller.

 
  1. public class HomeController : Controller  
  2. {  
  3.           
  4.     public ActionResult InlineHTMLHelper()  
  5.     {  
  6.         return View();  
  7.     }          
  8. }  
Right click action method and click add view.

 
  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. @helper OrderedList(string[] words)  
  6. {  
  7.     <ol>  
  8.         @foreach(string word in words)  
  9.         {  
  10.             <li>@word</li>  
  11.         }  
  12.     </ol>  
  13. }  
  14. <!DOCTYPE html>  
  15.   
  16. <html>  
  17. <head>  
  18.     <meta name="viewport" content="width=device-width" />  
  19.     <title>Inline HTML Helper</title>  
  20. </head>  
  21. <body>  
  22.     <div>   
  23.         @OrderedList(new string[]{"Delhi","Punjab","Assam","Bihar"})  
  24.   
  25.     </div>  
  26. </body>  
  27. </html>  
Preview

 

In the code shown above, we created an inline helper method i.e. OrderedList, which takes a string array as an argument and displays each word in an ordered list. We can reuse the inline HTML helpers multiple times on the same view.

In order to access the same inline HTML helper across the different view, we have to move our @helper methods in .cshtml files to App_Code folder.

  1. @helper OrderedList(string[] words)  
  2. {  
  3.     <ol>  
  4.         @foreach (string word in words)  
  5.         {  
  6.             <li>@word</li>  
  7.         }  
  8.     </ol>  
  9. }  
  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Inline HTML Helper App Code</title>  
  11. </head>  
  12. <body>  
  13.     <div>  
  14.         @InlineHelplerCode.OrderedList(new string[] { "Delhi", "Punjab", "Assam", "Bihar" })  
  15.     </div>  
  16. </body>  
  17. </html>  
Preview

 
 
Custom HTML Helpers

We can create custom HTML helpers in two ways,
  1. Using static methods
  2. Using extension methods
Using static methods

This is one of the simplest methods to create custom HTML helpers. We added a class (named as CustomHelper ) in CustomHelper folder.


  1. public static class CustomHelper    
  2. {    
  3.         public static MvcHtmlString Image(string source,string altTxt,string width,string height){    
  4.             //TagBuilder creates a new tag with the tag name specified    
  5.             var ImageTag = new TagBuilder("img");    
  6.             //MergeAttribute Adds attribute to the tag    
  7.             ImageTag.MergeAttribute("src", source);    
  8.             ImageTag.MergeAttribute("alt", altTxt);    
  9.             ImageTag.MergeAttribute("width", width);    
  10.             ImageTag.MergeAttribute("height", height);    
  11.             //Return an HTML encoded string with SelfClosing TagRenderMode    
  12.             return MvcHtmlString.Create(ImageTag.ToString(TagRenderMode.SelfClosing));    
  13.         }    
  14. }    
In the code shown above, we created a static method that returns an HTML-encoded string that uses MvcHtmlString. Now add namespace reference and call the custom helper from the view.
  1. @{  
  2.     Layout = null;  
  3. }  
  4. @using InlineAndCustomHTMLHelper.CustomHelper  
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Custom Static HTML Helper</title>  
  11. </head>  
  12. <body>  
  13.     <div>  
  14.         @CustomHelper.Image("/Images/UserPic.jpg","UserPic","100","100")  
  15.     </div>  
  16. </body>  
  17. </html>  
Preview



Using Extension Methods

Extension method enables us to add new methods to an existing class. To create an extension method, we have to create a static class, and first parameter of an extension method points towards the class that is extended by the method.
  1. public static MvcHtmlString Image(this HtmlHelper htmlhelper, string source, string altTxt, string width, string height)  
  2. {  
  3.     var ImageTag = new TagBuilder("img");  
  4.     ImageTag.MergeAttribute("src", source);  
  5.     ImageTag.MergeAttribute("alt", altTxt);  
  6.     ImageTag.MergeAttribute("width", width);  
  7.     ImageTag.MergeAttribute("height", height);  
  8.     return MvcHtmlString.Create(ImageTag.ToString(TagRenderMode.SelfClosing));  
  9. }  
In the code shown above, we added image custom helper method and extended the HtmlHelper class. Now go to view, add the namespace reference, and call image helper method from HTML Helper class.

code
  1. @{  
  2.     Layout = null;  
  3. }  
  4. @using InlineAndCustomHTMLHelper.CustomHelper  
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Custom Extension HTML Helper</title>  
  11. </head>  
  12. <body>  
  13.     <div>  
  14.         @Html.Image("/Images/UserPic.jpg","UserPic","100","100")  
  15.     </div>  
  16. </body>  
  17. </html>  
Preview

 

Now we can use Image HTML helper in all the views. The only thing we have to do is to add the reference at the top, so that we can call/invoke that custom helper method. If you want to use that custom helper method multiple times, you can add namespace of that custom helper class in the view’s web.config file.
  1. <add namespace="InlineAndCustomHTMLHelper.CustomHelper"/>


Similar Articles