我需要在RadioButtonList中显示我的列表,就像这样:@Html.RadioButtonList("FeatureList",new SelectList(ViewBag.Features)),但是你知道在HTML Helper类中没有RadioButtonList类,当我使用:@Html.RadioButton("FeatureList",new SelectList(ViewBag.Features))时,它会显示一个空白列表!//控制器代码:
public ActionResult Rules()
{
ViewBag.Features = (from m in Db.Features where m.ParentID == 3 select m.Name);
return View();
}发布于 2011-05-26 10:28:01
Html.RadioButton不接受(string, SelectList)参数,所以我认为应该是空白列表;)
你可以1)
对模型中的单选按钮值使用foreach,并使用Html.RadioButton(string, Object)重载来迭代值
// Options could be a List<string> or other appropriate
// data type for your Feature.Name
@foreach(var myValue in Model.Options) {
@Html.RadioButton("nameOfList", myValue)
}或2)
为list编写您自己的helper方法--可能是这样的(我从来没有写过这样的方法,所以您的里程可能会有所不同)
public static MvcHtmlString RadioButtonList(this HtmlHelper helper,
string NameOfList, List<string> RadioOptions) {
StringBuilder sb = new StringBuilder();
// put a similar foreach here
foreach(var myOption in RadioOptions) {
sb.Append(helper.RadioButton(NameOfList, myOption));
}
return new MvcHtmlString(sb.ToString());
}然后在视图中调用新的助手,比如(假设Model.Options仍然是List或其他适当的数据类型)
@Html.RadioButtonList("nameOfList", Model.Options)https://stackoverflow.com/questions/6132474
复制相似问题