So you need a group of ham radio fans? I use a special assistant for this.
Code for helper:
public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> listOfValues) { var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); var sb = new StringBuilder(); if (listOfValues != null) { // Create a radio button for each item in the list foreach (SelectListItem item in listOfValues) { // Generate an id to be given to the radio button field var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value); // Create and populate a radio button using the existing html helpers var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text)); var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString(); if (item.Selected) { radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id, @checked = "checked", }).ToHtmlString(); } // Create the html string that will be returned to the client // eg <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label> sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label); } } return MvcHtmlString.Create(sb.ToString()); }
Now, in your opinion, you can simply use:
@Html.RadioButtonForSelectList(model => model.yourproperty, Model.listUsedToPopulate)
Now you can check only one of the radio blocks. The verified value is stored in model.yourproperty.
source share