/ Published in: C#
Code snippet that allows developers to use a generic radiobuttonlist in the MVC Framework
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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) { 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.RenderBeginTag(HtmlTextWriterTag.Input); writer.Write(value); writer.RenderEndTag(); if (orientation == System.Web.UI.WebControls.Orientation.Vertical) { writer.RenderBeginTag(HtmlTextWriterTag.Br); writer.RenderEndTag(); } } } } }