- Posted by liammclennan on May 2, 2009
The default Asp.Net MVC View Helpers use method overloads to allow customisation of the generated output. Here is an example:
<%= Html.TextBox("Chapter_Slug", Model.Chapter.Slug, new { @class="required", title="Slug"}) %>
I have never been particularly happy with the conflicting method overloads and the use of the anonymous object to set attributes. Using MVCContrib you can use a fluent interface instead. The equivalent with fluent helpers is:
<%= this.TextBox(m => m.Chapter.Slug).Class("required").Title("Slug") %>
To use the MVC Contrib Fluent HTML Helpers:
- Get MVC Contrib and add a reference to MvcContrib.FluentHtml.dll.
- Inherit views from MvcContrib.FluentHtml.ModelViewPage<Model> and MvcContrib.FluentHtml.ModelViewUserControl<Model> instead of System.Web.Mvc.ViewPage<Model> and System.Web.Mvc.ViewUserControl<Model>.
- Use the fluent helpers on the view base classes instead of the HtmlHelper extension methods.