ASP.NET MVC With Fluent HTML Helpers

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:

  1. Get MVC Contrib and add a reference to MvcContrib.FluentHtml.dll.
  2. 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>.
  3. Use the fluent helpers on the view base classes instead of the HtmlHelper extension methods.

 


Comments are closed