在 ASP.NET MVC 应用程序中,我有两个单选按钮。根据模型中的 bool 值,如何启用或禁用单选按钮? (单选按钮的值也是模型的一部分)
我的单选按钮目前看起来像这样 -
@Html.RadioButtonFor(m => m.WantIt, "false")
@Html.RadioButtonFor(m => m.WantIt, "true")
在模型中,我有一个名为
model.Alive
的属性。 .如 model.Alive
是 true
我要启用 单选按钮,否则如果 model.Alive
是 false
,我要禁用 单选按钮。谢谢!
最佳答案
您可以直接将值作为 htmlAttributes 传递,如下所示:
@Html.RadioButtonFor(m => m.WantIt, "false", new {disabled = "disabled"})
@Html.RadioButtonFor(m => m.WantIt, "true", new {disabled = "disabled"})
如果你需要检查 model.Alive 那么你可以做这样的事情:
@{
var htmlAttributes = new Dictionary<string, object>();
if (Model.Alive)
{
htmlAttributes.Add("disabled", "disabled");
}
}
Test 1 @Html.RadioButton("Name", "value", false, htmlAttributes)
Test 2 @Html.RadioButton("Name", "value2", false, htmlAttributes)
希望有帮助
关于asp.net-mvc - ASP.NET MVC 禁用 Razor 语法的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14815821/