Return to Snippet

Revision: 50771
at September 2, 2011 02:02 by sommertim


Initial Code
public static partial class HtmlHelpers
{
 public static void ShowRadioButtonList<T>(this ViewPage page, IList<string> list, string name, Expression<Func<T, object>> valueProperty, Expression<Func<T, object>> displayProperty, string selectedValue, System.Web.UI.WebControls.Orientation orientation)
    {
        HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output);
        if (writer != null)
        {
            for (int i = 0; i < list.Count; i++)
            {                
                string value = list[i];
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, name + "_" + i);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, name, true);
                writer.AddAttribute(HtmlTextWriterAttribute.Value, value, true);
                if (value == selectedValue)
                    writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");

                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.Write(value);
                writer.RenderEndTag();

                if (orientation == System.Web.UI.WebControls.Orientation.Vertical)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Br);
                    writer.RenderEndTag();
                }
            }
        }
    }
}

Initial URL


Initial Description
Code snippet that allows developers to use a generic radiobuttonlist in the MVC Framework

Initial Title
MVC RadioButtonList

Initial Tags
extension

Initial Language
C#