MVC:: Creating Custom HTML Helpers
MVC:: Creating Custom HTML Helpers
The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that
you can use within your MVC views. By taking advantage of HTML Helpers, you can reduce
the amount of tedious typing of HTML tags that you must perform to create a standard
HTML page.
In the first part of this tutorial, I describe some of the existing HTML Helpers included with
the ASP.NET MVC framework. Next, I describe two methods of creating custom HTML
Helpers: I explain how to create custom HTML Helpers by creating a static method and by
creating an extension method.
<% } %>
</div>
</body>
</html>
The Html.BeginForm() Helper method is used to create the opening and closing HTML
<form> tags. Notice that the Html.BeginForm() method is called within a using statement.
The using statement ensures that the <form> tag gets closed at the end of the using block.
If you prefer, instead of creating a using block, you can call the Html.EndForm() Helper
method to close the <form> tag. Use whichever approach to creating an opening and
closing <form> tag that seems most intuitive to you.
The Html.TextBox() Helper methods are used in Listing 1 to render HTML <input> tags. If
you select view source in your browser then you see the HTML source in Listing 2. Notice
that the source contains standard HTML tags.
The ASP.NET MVC framework contains a small set of helpers. Most likely, you will need to
extend the MVC framework with custom HTML Helpers. In the remainder of this tutorial, you
learn two methods of creating custom HTML Helpers.
Listing 2 – Index.aspx Source
</form>
</div>
</body>
</html>
namespace MvcApplication1.Helpers
{
public class LabelHelper
{
public static string Label(string target, string text)
{
return String.Format("<label for='{0}'>{1}</label>",
target, text);
}
}
}
There is nothing special about the class in Listing 2. The Label() method simply returns a
string.
The modified Index view in Listing 3 uses the LabelHelper to render HTML <label> tags.
Notice that the view includes an <%@ Imports %> directive that imports the
Application1.Helpers namespace.
Listing 2 – Views\Home\Index2.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Index2.aspx.cs"
Inherits="MvcApplication1.Views.Home.Index2" %>
<%@ Import Namespace="MvcApplication1.Helpers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index2</title>
</head>
<body>
<div>
<% } %>
</div>
</body>
</html>
namespace MvcApplication1.Helpers
{
public static class LabelExtensions
{
public static string Label(this HtmlHelper helper, string
target, string text)
{
return String.Format("<label for='{0}'>{1}</label>",
target, text);
}
}
}
After you create an extension method, and build your application successfully, the extension
method appears in Visual Studio Intellisense like all of the other methods of a class (see
Figure 2). The only difference is that extension methods appear with a special symbol next
to them (an icon of a downward arrow).
Figure 2 – Using the Html.Label() extension method
The modified Index view in Listing 4 uses the Html.Label() extension method to render all of
its <label> tags.
Listing 4 – Views\Home\Index3.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Index3.aspx.cs"
Inherits="MvcApplication1.Views.Home.Index3" %>
<%@ Import Namespace="MvcApplication1.Helpers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index3</title>
</head>
<body>
<div>
<% } %>
</div>
</body>
</html>
Summary
In this tutorial, you learned two methods of creating custom HTML Helpers. First, you
learned how to create a custom Label() HTML Helper by creating a static method that
returns a string. Next, you learned how to create a custom Label() HTML Helper method by
creating an extension method on the HtmlHelper class.
In this tutorial, I focused on building an extremely simple HTML Helper method. Realize that
an HTML Helper can be as complicated as you want. You can build HTML Helpers that render
rich content such as tree views, menus, or tables of database data.