MVC 2 Editor Template for Radio Buttons

A while back I blogged about how to create an HTML Helper to produce a radio button list.  In that post, my HTML helper was "wrapping" the FluentHtml library from MvcContrib to produce the following html output (given an IEnumerable list containing the items "Foo" and "Bar"):

<div>
    <input id="Name_Foo" name="Name" type="radio" value="Foo" /><label for="Name_Foo" id="Name_Foo_Label">Foo</label>
    <input id="Name_Bar" name="Name" type="radio" value="Bar" /><label for="Name_Bar" id="Name_Bar_Label">Bar</label>
</div>

With the release of MVC 2, we now have editor templates we can use that rely on metadata to allow us to customize our views appropriately.  For example, for the radio buttons above, we want the "id" attribute to be differentiated and unique and we want the "name" attribute to be the same across radio buttons so the buttons will be grouped together and so model binding will work appropriately. We also want the "for" attribute in the element being set to correctly point to the id of the corresponding radio button.  The default behavior of the RadioButtonFor() method that comes OOTB with MVC produces the same value for the "id" and "name" attributes so this isn't exactly what I want out the the box if I'm trying to produce the HTML mark up above.

If we use an EditorTemplate, the first gotcha that we run into is that, by default, the templates just work on your view model's property. But in this case, we also was the list of items to populate all the radio buttons. It turns out that the EditorFor() methods do give you a way to pass in additional data. There is an overload of the EditorFor() method where the last parameter allows you to pass an anonymous object for "extra" data that you can use in your view – it gets put on the view data dictionary:

<%: Html.EditorFor(m => m.Name, "RadioButtonList", new { selectList = new SelectList(new[] { "Foo", "Bar" }) })%>

Now we can create a file called RadioButtonList.ascx that looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    var list = this.ViewData["selectList"] as SelectList;
%>
<div>
    <% foreach (var item in list) {
           var radioId = ViewData.TemplateInfo.GetFullHtmlFieldId(item.Value);
           var checkedAttr = item.Selected ? "checked="checked"" : string.Empty;
    %>
        <input type="radio" id="<%: radioId %>" name="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>" value="<%: item.Value %>" <%: checkedAttr %>/>
        <label for="<%: radioId %>"><%: item.Text %></label>
    <% } %>
</div>

There are several things to note about the code above. First, you can see in line #3, it's getting the SelectList out of the view data dictionary. Then on line #7 it uses the GetFullHtmlFieldId() method from the TemplateInfo class to ensure we get unique IDs. We pass the Value to this method so that it will produce IDs like "Name_Foo" and "Name_Bar" rather than just "Name" which is our property name. However, for the "name" attribute (on line #10) we can just use the normal HtmlFieldPrefix property so that we ensure all radio buttons have the same name which corresponds to the view model's property name. We also get to leverage the fact the a SelectListItem has a Boolean Selected property so we can set the checkedAttr variable on line #8 and use it on line #10. Finally, it's trivial to set the correct "for" attribute for the on line #11 since we already produced that value.

Because the TemplateInfo class provides all the metadata for our view, we're able to produce this view that is widely re-usable across our application. In fact, we can create a couple HTML helpers to better encapsulate this call and make it more user friendly:

public static MvcHtmlString RadioButtonList<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, params string[] items)
{
    return htmlHelper.RadioButtonList(expression, new SelectList(items));
}
 
public static MvcHtmlString RadioButtonList<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items)
{
    var func = expression.Compile();
    var result = func(htmlHelper.ViewData.Model);
    var list = new SelectList(items, "Value", "Text", result);
    return htmlHelper.EditorFor(expression, "RadioButtonList", new { selectList = list });
}

This allows us to simply the call like this:

<%: Html.RadioButtonList(m => m.Name, "Foo", "Bar" ) %>

In that example, the values for the radio button are hard-coded and being passed in directly. But if you had a view model that contained a property for the collection of items you could call the second overload like this:

<%: Html.RadioButtonList(m => m.Name, Model.FooBarList ) %>

The Editor templates introduced in MVC 2 definitely allow for much more flexible views/editors than previously available. By knowing about the features you have available to you with the TemplateInfo class, you can take these concepts and customize your editors with extreme flexibility and re-usability.