0

我有一个包含 8 个不同单选按钮组(“是/否”按钮组)的页面。最初我已经编写了 html 以在选中“否”按钮的情况下进行渲染,但现在我需要在未选择任何按钮的情况下进行渲染。

我删除了“已检查 = 已检查”并刷新,但仍选择了“否”按钮。我再次刷新,清除缓存,并在不同的浏览器上运行无济于事。我正在寻求帮助找出可能的原因。

我的 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");
         }
        });
    });
4

2 回答 2

1

如果您没有初始化 IsFelon,那么默认情况下,它将设置为“False”,这是布尔类型对象的本质。

另一方面,如果您希望默认情况下不选择收音机,那么您不应该使用 bool,您应该使用“bool”?它将默认值设置为“null”,因此不会选择任何无线电。

于 2012-12-04T00:58:53.333 回答
0

您的模型的属性是否IsFelon有价值?如果它是标准布尔值,它会。因此,当您将该属性映射到值为 False 的单选按钮时,MVC 会识别它们匹配并将按钮设置为checked.

我能想到的最简单的解决方案是不将这些单选按钮映射到模型属性,而只是检索这些值并在回发时在控制器中以编程方式设置模型属性。您也可以考虑创建IsFelon一个可为空的属性,但在 MVC 中使用可为空的属性可能会涉及一些摆弄。

于 2012-12-03T23:23:05.533 回答