本文介绍了RadiobuttonFor Mvc Razor语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个单选按钮,并且字段值类型为整数,例如维护3",对1有效,对2不活动.

I have three radio buttons and my field value type is integer like Maintenance for 3,Active for 1 and Inactive for 2.

@Html.RadioButtonFor(model => model.StatusId, "3") Maintenance
@if (Model.IsReady == true)
{
    <span> @Html.RadioButtonFor(model => model.StatusId,"1") Active</span>
}
@Html.RadioButtonFor(model => model.StatusId, "2") Inactive

我使用上面的代码然后正确插入数据,但是当我的表单在编辑模式下打开时,我没有选择任何单选按钮.

i using above code then insert data properly but when my form open in edit mode then i have not got any radio button selected.

我也使用了下面的代码,但没有成功.

and i also used below code but not get success.

@Html.RadioButtonFor(model => model.StatusId, "3", Model.StatusId == '3' ? new {Checked = "checked"} : null) Maintenance
@if (Model.IsReady == true)
{
    <span> @Html.RadioButtonFor(model => model.StatusId, "1",Model.StatusId == '1' ? new {Checked = "checked"} : null) Active</span>
}
@Html.RadioButtonFor(model => model.StatusId, "2",Model.StatusId == '2' ? new {Checked = "checked"} : null) Inactive

那么如何在编辑模式下获得选中的单选按钮

so how to get selected radio button in edit mode

预先感谢

推荐答案

我创建单选按钮的方式如下:

The way I create radiobuttons is as follows:

@Html.RadioButtonFor(model => model.StatusId,3,new { id = "rbMaintenance" })
@Html.Label("rbMaintenance","Maintenance")
@Html.RadioButtonFor(model => model.StatusId,2,new { id = "rbInactive" })
@Html.Label("rbInactive","Inactive")
@Html.RadioButtonFor(model => model.StatusId,1,new { id = "rbActive" })
@Html.Label("rbActive","Active")

与您的代码的区别实际上是,由于您的StatusId类型为Int,因此您应该在RadioButtonFor中以整数形式输入值,例如3表示维护,而不是"3".

The difference to your code really is that as your StatusId type is Int then you should enter the value in the RadioButtonFor as an integer e.g. 3 for Maintenance rather than '3'.

这篇关于RadiobuttonFor Mvc Razor语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 00:48