Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the usage in the aspx page</p> <pre><code> &lt;%= Html.RadioButtonListFor(m =&gt; m.GenderRadioButtonList)%&gt; </code></pre> <p>Here is the view model</p> <pre><code>public class HomePageViewModel { public enum GenderType { Male, Female } public RadioButtonListViewModel&lt;GenderType&gt; GenderRadioButtonList { get; set; } public HomePageViewModel() { GenderRadioButtonList = new RadioButtonListViewModel&lt;GenderType&gt; { Id = "Gender", SelectedValue = GenderType.Male, ListItems = new List&lt;RadioButtonListItem&lt;GenderType&gt;&gt; { new RadioButtonListItem&lt;GenderType&gt;{Text = "Male", Value = GenderType.Male}, new RadioButtonListItem&lt;GenderType&gt;{Text = "Female", Value = GenderType.Female} } }; } } </code></pre> <p>Here's the view model used for radio button lists</p> <pre><code>public class RadioButtonListViewModel&lt;T&gt; { public string Id { get; set; } private T selectedValue; public T SelectedValue { get { return selectedValue; } set { selectedValue = value; UpdatedSelectedItems(); } } private void UpdatedSelectedItems() { if (ListItems == null) return; ListItems.ForEach(li =&gt; li.Selected = Equals(li.Value, SelectedValue)); } private List&lt;RadioButtonListItem&lt;T&gt;&gt; listItems; public List&lt;RadioButtonListItem&lt;T&gt;&gt; ListItems { get { return listItems; } set { listItems = value; UpdatedSelectedItems(); } } } public class RadioButtonListItem&lt;T&gt; { public bool Selected { get; set; } public string Text { get; set; } public T Value { get; set; } public override string ToString() { return Value.ToString(); } } </code></pre> <p>Here's the extension methods for RadioButtonListFor</p> <pre><code>public static class HtmlHelperExtensions { public static string RadioButtonListFor&lt;TModel, TRadioButtonListValue&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, RadioButtonListViewModel&lt;TRadioButtonListValue&gt;&gt;&gt; expression) where TModel : class { return htmlHelper.RadioButtonListFor(expression, null); } public static string RadioButtonListFor&lt;TModel, TRadioButtonListValue&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, RadioButtonListViewModel&lt;TRadioButtonListValue&gt;&gt;&gt; expression, object htmlAttributes) where TModel : class { return htmlHelper.RadioButtonListFor(expression, new RouteValueDictionary(htmlAttributes)); } public static string RadioButtonListFor&lt;TModel, TRadioButtonListValue&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, RadioButtonListViewModel&lt;TRadioButtonListValue&gt;&gt;&gt; expression, IDictionary&lt;string, object&gt; htmlAttributes) where TModel : class { var inputName = GetInputName(expression); RadioButtonListViewModel&lt;TRadioButtonListValue&gt; radioButtonList = GetValue(htmlHelper, expression); if (radioButtonList == null) return String.Empty; if (radioButtonList.ListItems == null) return String.Empty; var divTag = new TagBuilder("div"); divTag.MergeAttribute("id", inputName); divTag.MergeAttribute("class", "radio"); foreach (var item in radioButtonList.ListItems) { var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem{Text=item.Text, Selected = item.Selected, Value = item.Value.ToString()}, htmlAttributes); divTag.InnerHtml += radioButtonTag; } return divTag + htmlHelper.ValidationMessage(inputName, "*"); } public static string GetInputName&lt;TModel, TProperty&gt;(Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { if (expression.Body.NodeType == ExpressionType.Call) { var methodCallExpression = (MethodCallExpression)expression.Body; string name = GetInputName(methodCallExpression); return name.Substring(expression.Parameters[0].Name.Length + 1); } return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1); } private static string GetInputName(MethodCallExpression expression) { // p =&gt; p.Foo.Bar().Baz.ToString() =&gt; p.Foo OR throw... var methodCallExpression = expression.Object as MethodCallExpression; if (methodCallExpression != null) { return GetInputName(methodCallExpression); } return expression.Object.ToString(); } public static string RadioButton(this HtmlHelper htmlHelper, string name, SelectListItem listItem, IDictionary&lt;string, object&gt; htmlAttributes) { var inputIdSb = new StringBuilder(); inputIdSb.Append(name) .Append("_") .Append(listItem.Value); var sb = new StringBuilder(); var builder = new TagBuilder("input"); if (listItem.Selected) builder.MergeAttribute("checked", "checked"); builder.MergeAttribute("type", "radio"); builder.MergeAttribute("value", listItem.Value); builder.MergeAttribute("id", inputIdSb.ToString()); builder.MergeAttribute("name", name + ".SelectedValue"); builder.MergeAttributes(htmlAttributes); sb.Append(builder.ToString(TagRenderMode.SelfClosing)); sb.Append(RadioButtonLabel(inputIdSb.ToString(), listItem.Text, htmlAttributes)); sb.Append("&lt;br&gt;"); return sb.ToString(); } public static string RadioButtonLabel(string inputId, string displayText, IDictionary&lt;string, object&gt; htmlAttributes) { var labelBuilder = new TagBuilder("label"); labelBuilder.MergeAttribute("for", inputId); labelBuilder.MergeAttributes(htmlAttributes); labelBuilder.InnerHtml = displayText; return labelBuilder.ToString(TagRenderMode.Normal); } public static TProperty GetValue&lt;TModel, TProperty&gt;(HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) where TModel : class { TModel model = htmlHelper.ViewData.Model; if (model == null) { return default(TProperty); } Func&lt;TModel, TProperty&gt; func = expression.Compile(); return func(model); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload