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: }