HTML and CSS Reference
In-Depth Information
namespace System.Web.Mvc
{
public class Html5Helper
{
private readonly HtmlHelper htmlHelper;
public Html5Helper(HtmlHelper htmlHelper)
{
this.htmlHelper = htmlHelper;
}
private static CultureInfo Culture
{
get
{
return CultureInfo.CurrentCulture;
}
}
// Add custom methods hereā€¦
}
public static class HtmlHelperExtension
{
public static Html5Helper Html5(this HtmlHelper instance)
{
return new Html5Helper(instance);
}
}
}
here are a couple of things to point out here. First, note that the namespace is set as System.Web.Mvc and
not your application's namespace, Chapter 3 . Your custom helper class is named Html5Helper and its constructor
takes an HtmlHelper parameter. This is a reference to the standard helper class, which is stored as a private
class member. Your custom methods will need this to access data from the framework such a view and model
information. Finally, this code also declares a static HtmlHelperExtension class, which provides a static method
that returns your custom class. Notice that the method name is Html5 , so you will access your custom class from
the view as:
@Html.Html5().<CustomMethod>()
The purpose of having your own custom helper class is to be able to implement custom helper methods.
So let's add one now. The first method will generate an email input control. You will then use this in your
EmailAddress.cshtml template.
3.
Add the code shown in listing 3-6 to your custom class where the // Add custom
methods here placeholder is.
Listing 3-6. The EmailControl implementation
public IHtmlString EmailControl()
{
string id;
string name;
Search WWH ::




Custom Search