我在模型中有一个名为“TypeOfProperty”的int类型的属性。视图页面上有几个单选按钮。我想根据“TypeOfProperty”的值检查相应的单选按钮(如“公寓”,“情节”等)。
@Html.RadioButton("Appartment", @Model.TypeOfProperty.Equals("1"))
答案 0 :(得分: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。
答案 1 :(得分:0)
在您的控制器操作中,您可以运行此代码以获取@Model.TypeOfProperty.Equals("1")
Request.Form["Appartment"]
答案 2 :(得分:0)
@Html.RadioButton("Appartment",@Model.TypeOfProperty, @Model.TypeOfProperty.Equals("1"))
第三个参数是isChecked
。如果此表达式的计算结果为真,则将选择无线电。