(+N) Consulting Inc.

Consulting. Software solutions. Training.

MVC Radio Button List

As predicted, I came around to using some radio  buttons. As you might guess by now, I didn't like the HTML or the implementation in the current MVC release. As you may expect, I wrote my own :-) The implementation is fairly simple and straightforward. It extends System.Web.MVC.ViewPage, takes a list of objects, allows for selection of one of the radio buttons, supports orientation and supports selection of both the value the radio button submits and the display string for that item independently. 1: using System; 2: using System.Collections.Generic; 3: using System.Linq.Expressions; 4: using System.Web.Mvc; 5: using System.Web.UI; 6:  7: public static partial class HtmlHelpers 8: { 9: public static void ShowRadioButtonList<T>(this ViewPage page, IList<T> list, string name, Expression<Func<T, object>> valueProperty, Expression<Func<T, object>> displayProperty, System.Web.UI.WebControls.Orientation orientation) 10: { 11: page.ShowRadioButtonList(list, name, valueProperty, displayProperty, "3", orientation); 12: } 13:  14:  15: public static void ShowRadioButtonList<T>(this ViewPage page, IList<T> list, string name, Expression<Func<T, object>> valueProperty, Expression<Func<T, object>> displayProperty, string selectedValue, System.Web.UI.WebControls.Orientation orientation) 16: { 17: HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output); 18: if (writer != null) 19: { 20: Func<T, object> valueGetter = valueProperty.Compile(); 21: Func<T, object> displayStringGetter = displayProperty.Compile(); 22:  23: for (int i = 0; i < list.Count; i++) 24: { 25: T row = list[i]; 26: string value = valueGetter(row).ToString(); 27: writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio"); 28: writer.AddAttribute(HtmlTextWriterAttribute.Id, name + "_" + i); 29: writer.AddAttribute(HtmlTextWriterAttribute.Name, name, true); 30: writer.AddAttribute(HtmlTextWriterAttribute.Value, value, true); 31: if (value == selectedValue) 32: { 33: writer.AddAttribute(HtmlTextWriterAttribute.Checked,"checked"); 34: } 35: writer.RenderBeginTag(HtmlTextWriterTag.Input); 36: writer.Write(displayStringGetter(row)); 37: writer.RenderEndTag(); 38:  39: if (orientation == System.Web.UI.WebControls.Orientation.Vertical) 40: { 41: writer.RenderBeginTag(HtmlTextWriterTag.Br); 42: writer.RenderEndTag(); 43: } 44: } 45: } 46: } 47: }   This implementation uses the type Expression<Func<T,Object>> for the selection of the value and the display string. To invoke the helper, assuming you have a list of Product objects, Product being defined: 1: public class Product 2: { 3: public int ProductID { get; set; } 4: public string Title { get; set; } 5: public string Description { get; set; } 6: public decimal Price { get; set; } 7: } You can now call the helper from your MVC View Page strongly typed with a model List<Product>: 1: <%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<List<Product>>" 2: MasterPageFile="~/Views/Shared/Site.Master" %> 3:  invoked simply 1: <% this.ShowRadioButtonList<Product>(Model, 2: "productRadioButtonList", 3: a => a.ProductID, 4: d => d.Title, "4", 5: Orientation.Horizontal); %> This call will create a radio button for each product in the list (Model is a List<Product> because we strongly typed the page as such). The radio button will use the ProductID property for the value of the input, and the Title property as the displayed string. If one of the product Id's happens to be 4, that radio button will be selected in the group. The radio buttons will all be named "productRadioButtonList" but their ID will be appended an ordinal number  "productRadioButtonList_1","productRadioButtonList_2","productRadioButtonList_3" etc. There you have it: another day, another MVC control, another blog post.