如何将使用Html.RadioButton()
HTML帮助程序生成的单选按钮的值绑定到具有结构类型的字段?
不太抽象:
CommonProject.Services.SearchBag.Effects:
public enum Effects
{
Any,
Solid,
Effect
}
在强类型的ViewData中:
public class SearchBag{
public Effects EffectIndicator { get; set; }
}
在我看来(这不起作用):
<%=Html.RadioButton("SearchBag.EffectIndicator", "Any", ViewData.Model.SearchBag.EffectIndicatorIsAny, new { @id = "SearchBag.EffectIndicatorAny" })%>
更新
它似乎工作一次..
最初它会根据需要创建radiobuttons,然后当您更改值并回发时,该值将被正确绑定。然后在重新生成页面时,按钮的所有值都将设置为您之前选择的值。
答案 0 :(得分:1)
如果您的View强烈输入SearchBag作为您的视图数据类,那么您应该可以按照这些方式执行某些操作:
<%= Html.RadioButtonFor(model => model.EffectIndicator, "Any", new { @id = "SearchBag.EffectIndicatorAny" }) %>
然后,当您的View表单提交回Controller时,它看起来像:
public class MyController : Controller
{
public ActionResult MyActionMethod(SearchBag searchBag)
{
Effects selectedEffect = searchBag.EffectIndicator;
}
}
这有帮助吗?