[英]@Html.RadioButtonFor Default to Unchecked
In ASP.NET MVC4's Razor view, how to ensure none of the radio buttons default to unchecked? 在ASP.NET MVC4的Razor视图中,如何确保没有任何单选按钮默认未选中? MVC seems to check one for me even I don't declare to check one in the following codes. MVC似乎为我检查了一个,即使我没有声明在以下代码中检查一个。
@Html.RadioButtonFor(model => model.test, true, new { id = "radio_test_True"}) Yes
@Html.RadioButtonFor(model => model.test, false, new { id = "radio_test_False"}) No
Thanks 谢谢
Set Model.test
to null in your controller or model constructor, and make sure it's nullable. 在控制器或模型构造函数Model.test
设置为null,并确保它可以为空。 A bool
has to be either true or false, null
will mean neither is checked. bool
必须是true或false, null
表示不检查。
public class WhateverTheModelIs
{
public bool? test { get; set; }
public WhateverTheModelIs()
{
test = null;
}
}
Note: Setting the value to null
is redundant as it is the default. 注意:将值设置为null
是多余的,因为它是默认值。 I wanted to be explicit for the sake of the example. 为了这个例子,我想明确一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.