【问题标题】:How to check the radio buttons using the value of a hidden field with Mvc?如何使用 Mvc 的隐藏字段的值检查单选按钮?
【发布时间】:2014-09-02 05:17:55
【问题描述】:
我在模型中有一个名为“TypeOfProperty”的 int 类型属性。查看页面上有几个单选按钮。我想根据“TypeOfProperty”的值检查相应的单选按钮(如“Appartment”、“plot”等)。
@Html.RadioButton("Appartment", @Model.TypeOfProperty.Equals("1"))
【问题讨论】:
标签:
asp.net
asp.net-mvc
asp.net-mvc-3
asp.net-mvc-4
【解决方案1】:
我认为您误解了 RadioButton 助手的工作原理。如果您有int TypeOfProperty 并且您想回发选定的值,那么它将类似于
@Html.RadioButtonFor(m => m.TypeOfProperty, "1")<span>Apartment</span>
@Html.RadioButtonFor(m => m.TypeOfProperty, "2")<span>Plot</span>
@Html.RadioButtonFor(m => m.TypeOfProperty, "3")<span>Another type</span>
如果TypeOfProperty 的值为2,则第二个单选按钮将被选中。如果用户选择了第一个单选按钮,那么TypeOfProperty 将返回值为 1。
【解决方案2】:
在控制器的操作中,您可以运行此代码以获取 @Model.TypeOfProperty.Equals("1") 的值
Request.Form["Appartment"]
【解决方案3】:
@Html.RadioButton("Appartment",@Model.TypeOfProperty, @Model.TypeOfProperty.Equals("1"))
第三个参数是isChecked。如果此表达式的计算结果为真,则将选择收音机。