Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have implemented something similar in MVC 1.0. See if this will be helpful for you:</p> <pre><code> public static string RadioButtonList2(this HtmlHelper _helper, string _name, IEnumerable&lt;SelectListItem&gt; _items, string _selectedValue, string _seperator) { return RadioButtonList2(_helper, _name, _items, _selectedValue, _seperator, null); } public static string RadioButtonList2(this HtmlHelper _helper, string _name, IEnumerable&lt;SelectListItem&gt; _items, string _selectedValue, string _seperator, IDictionary&lt;string, object&gt; _htmlAttributes) { StringBuilder _outputScript = new StringBuilder(); foreach (var item in _items) { var optionField = new TagBuilder("input"); optionField.MergeAttribute("name", _name); optionField.MergeAttribute("id", _name); optionField.MergeAttribute("class", _name); optionField.MergeAttribute("value", item.Value); optionField.MergeAttribute("type", "radio"); // Check to see if it's checked if (item.Value == _selectedValue) optionField.MergeAttribute("checked", "checked"); if (_htmlAttributes != null) optionField.MergeAttributes(_htmlAttributes); _outputScript.Append(optionField.ToString(TagRenderMode.SelfClosing)); _outputScript.Append("&lt;label style=\"display:inline;\"&gt;"); _outputScript.Append(item.Text); _outputScript.Append("&lt;/label&gt;" + _seperator); } return _outputScript.ToString(); } </code></pre> <p>In the controller, you can return the result as follows:</p> <pre><code> ViewData["GenderList"] = new SelectList(new[] { new { Value = "M", Text = "Male" }, new { Value = "F", Text = "Female" }, new { Value = "A", Text = "All" } }, "Value", "Text"); </code></pre> <p>or</p> <pre><code> ViewData["GenderList"] = new SelectList(_resultFromSomeLinqQuery, "GenderID", "GenderName"); </code></pre> <p>And use it in the View as follows:</p> <pre><code>&lt;%= Html.RadioButtonList2("Sex", ViewData["GenderList"] as SelectList, ViewData["SelectedSex"].ToString(), "&amp;nbsp;")%&gt; </code></pre> <p>You can also replace the <code>&amp;nbsp;</code> with <code>&lt;BR /&gt;</code> to display them in seperate lines.</p> <p>Hope this helps.</p> <p>Regards Naweed Akram naweed@xgeno.com</p>
 

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