我有一个页面与8个不同的单选按钮组(‘是/否’按钮组)。最初,我将html编码为在选中“no”按钮的情况下进行渲染,但现在我需要在不选中按钮的情况下进行渲染。
我删除了“checked=checked”并进行了刷新,但NO按钮仍然处于选中状态。我再次刷新,清除了缓存,并在不同的浏览器上运行,但都无济于事。我正在寻找帮助确定可能的原因。
我的MVC代码:
@Html.RadioButtonFor(m => m.IsFelon, true, new { @class = "commentBox RadioIf" })
<label for="PersonModel_Felon">Yes</label>
@Html.RadioButtonFor(m => m.IsFelon, false, new { @class = "commentBox" })
<label for="PersonModel_Felon">No</label>它的渲染方式:
<input class="commentBox" data-val="true" data-val-required="The ChildAbusePast field is required." id="ChildAbusePast" name="ChildAbusePast" type="radio" value="True" />
<label for="HistoryModel_ChildAbusePast">Yes</Label>
<input checked="checked" class="commentBox" id="ChildAbusePast" name="ChildAbusePast" type="radio" value="False" />
<label for="HistoryModel_ChildAbusePast">No</Label>唯一涉及到这一点的是一些jquery:
$(document).ready(function () {
$("input.commentBox").click(function () {
if ($(this).closest('div.editor-field2').children('input:checked').val() == "True") {
$(this).closest('div.formQuestion').children('div.hidden').slideDown(800);
} else {
$(this).closest('div.formQuestion').children('div.hidden').slideUp("fast");
}
});
});发布于 2012-12-04 08:58:53
如果您没有初始化IsFelon,那么在默认情况下,它将被设置为"False“,这是boolean类型的object的本质。
另一方面,如果你想在默认情况下取消选择无线电,那么你不应该使用bool,你应该使用"bool?“其将缺省值设置为"null“,因此将不会选择任何无线电。
发布于 2012-12-04 07:23:06
您的模型的IsFelon属性是否有值?如果它是一个标准的布尔值,它就会。因此,当您将该属性映射到值为False的单选按钮时,MVC会识别出它们是匹配的,并将该按钮设置为checked。
我能想到的最简单的解决方案是不将这些单选按钮映射到模型属性,而只是检索这些值并在回发时在控制器中以编程方式设置模型属性。您还可以考虑将 IsFelon 设为可为空的属性,但在 MVC 中使用可为空的属性可能会涉及一些麻烦。
https://stackoverflow.com/questions/13693441
复制相似问题