Stepwise Creating Custom HTML Helper In MVC

We know HTML helpers are like traditional ASP.NET controls. As we create the custom controls we can also create the custom helpers.

How to create the custom helpers we will see as in the following steps,

Step 1: Create your MVC application.

Step 2: Add folder HTMLHelper and class HTMLHelper ( you may give your project a specific name )

HTMLHelper

Step 3: Now we are creating helper which will convert text in to capital letter and create label.

So add extension method for htmlhelper as below. Return type must be IHTMLString.

Note: I have created basic helper; you may create some complex multi parameter helper as per your need.

  1. public static class HTMLHelper  
  2. {  
  3.    public static IHtmlString DisplayCapitalText(this HtmlHelper htmlHelper,  
  4.    string name, string value)  
  5.    {  
  6.       return new HtmlString(string.Format("<label id=\"" + name + "\"> " + value.ToUpper() + "</label>"));  
  7.    }  
  8. }  
Step 4: Build project and in Web.config add the namespace of helper class.

HTML

Build the solution

Step 5: Build the solution, close and reopen it.

Step 6: On UI you can access your helper as in the following,

UI

I have given sample string as in the following,

string

Step 7:
Run the project and you can see the string in capital format

Create Student

 

Up Next
    Ebook Download
    View all
    Learn
    View all