Building with the HtmlHelperContext

Using a "helper context" allows you to add things like CSS classes and html attributes to the context object once, and have them repeated on each builder that is created from the context.

In this example, we are adding the [data-theme="b"] attribute to all of the builders created by @builder.

@using (Html.BuildForm().Begin())
{
    using (var @builder = Html.NewHelperContext())
    {
        builder.Data("theme", "b");
        
    <ul data-role="listview" data-inset="true">
        <li data-role="list-divider">Details</li>
        <li data-role="fieldcontain">
            @builder.BuildLabelFor(m => m.UserName)
            @builder.BuildTextBoxFor(m => m.UserName).Placeholder("enter username")
            @builder.BuildValidationMessageFor(m => m.UserName)
        </li>
        <li data-role="fieldcontain">
            @builder.BuildLabelFor(m => m.Password)
            @builder.BuildPasswordFor(m => m.Password).Placeholder("enter password")
            @builder.BuildValidationMessageFor(m => m.Password)
        </li>
        <li data-role="fieldcontain">
            @builder.BuildLabelFor(m => m.RememberMe)
            @builder.BuildCheckBoxFor(m => m.RememberMe)
        </li>
        <li data-role="fieldcontain">
            @builder.BuildSubmitButton("Log On")
        </li>
    </ul>
    }
}

Last edited Dec 21, 2011 at 6:00 AM by grcodemonkey, version 1

Comments

No comments yet.