Using Asp.net MVC, we can having total control over what and how your code is rendered in the browser. When you create the HTML for your page, a class that is very helpful is the HtmlHelper class. This class represents support for rendering HTML controls in a view. You can use the HtmlHelper class to render controls such as a TextBox, which renders as input type=”text”, to the browser like the following:
This article will show you how to create your own extension method to complement the existing HtmlHelper class.
In existing MVC project create a new folder in the root directory called Common. In that folder create a new class called Helpers and add the following code:
public static class Helpers
public static string Span(this HtmlHelper html, string text)
var builder = new TagBuilder("span");
The next step is to make this helper class available to all the Views in the project. The better option is to add the namespace to the web.config file so it’s available to every view.
This helper class can now be used through the entire project. To demonstrate how to use this helper class you need to open the About.chtml and add the following code:
In the code above there is the Span extension method created earlier. As you might expect it will render a tag in the browser.
You can create helper methods that map to different HTML tags, so you can create them to display bold or even italic text. The choice is up to you. This is a nice feature of MVC that allows you to easily build custom HTML tags in your views.