每当我需要从查询创建的单选按钮列表时,我总是会使用此 RadioButtonListFor 扩展方法。奇迹般有效:
// jonlanceley.blogspot.com/2011/06/mvc3-radiobuttonlist-helper.html
public static MvcHtmlString RadioButtonListFor<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();
sb.Append("<span class='RadioButtonListFor'> ");
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 htmlAttributes = new Dictionary<string, object>();
htmlAttributes.Add("id", id);
if (item.Selected)
htmlAttributes.Add("checked", "checked");
var radio = htmlHelper.RadioButtonFor(expression, item.Value, htmlAttributes);
// Create the html string that will be returned to the client
// e.g. <label<input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" />Line1</label>
sb.AppendFormat("<label>{0} {1}</label> ", radio, HttpUtility.HtmlEncode(item.Text));
}
}
sb.Append(" </span>");
return MvcHtmlString.Create(sb.ToString());
}
现在,您可以从内存中的任何集合生成单选按钮,通常作为 ViewModel 上的属性,如下所示:
public int SelectedPaymentMethodId { get; set; }
public IEnumerable<SelectListItem> PaymentMethodChoices
{
get
{
return from x in dataSourceFoo
select new SelectListItem {
Text = x.TextThing, Value = x.Id, Selected = (x.Id == SelectedPaymentMethodId)
};
}
}
你的视图很简单:
@Html.RadioButtonListFor(model => model.SelectedPaymentMethodId, Model.PaymentMethodChoices)