简体   繁体   English

单选按钮呈现为“ checked = checked”,不确定来源

[英]Radio buttons are rendering as 'checked=checked', not sure of source

I have a page with 8 different radio button groups (groups of 'yes/no' buttons). 我的页面上有8个不同的单选按钮组(“是/否”按钮组)。 Originally i had coded the html to render with the 'No' button checked, but now I need to render with no buttons selected. 最初,我已将html编码为在选中“否”按钮的情况下进行渲染,但是现在我需要在未选择按钮的情况下进行渲染。

I removed the 'checked = checked' and refreshed but the NO buttons were still selected. 我删除了“已检查=已检查”并刷新,但仍选择了“否”按钮。 I have refreshed again, cleared the cache, and run on a different browser to no avail. 我再次刷新,清除了缓存,并在其他浏览器上运行无济于事。 I am looking for assistance locating possible causes. 我正在寻找可能的原因的协助。

My MVC code: 我的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>

How it renders: 呈现方式:

<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>

Only other thing touching this is some jquery: 唯一涉及到此的是一些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");
         }
        });
    });

If you didn't initialise IsFelon, then by default, it will be set to "False" which is the nature of the boolean type of object. 如果未初始化IsFelon,则默认情况下会将其设置为“ False”,这是对象的布尔类型的性质。

On the other hand, if you want the radios to be unselected by default, then you shouldn't be using bool, you would want to use "bool?" 另一方面,如果您希望默认情况下取消选中广播,那么您不应该使用bool,而是要使用“ bool?”。 which sets the default value to "null" and thus none of the radios will be selected. 它将默认值设置为“ null”,因此将不会选择任何无线电。

Does the IsFelon property of your model have a value? 您模型的IsFelon属性是否有值? If it's a standard boolean, it will. 如果是标准布尔值,它将。 Thus when you map that property to the radio button whose value is False, MVC recognizes that they match and sets the button to checked . 因此,当您将该属性映射到值为False的单选按钮时,MVC会识别出它们匹配,并将按钮设置为checked

The easiest solution I can think of is to not map those radio buttons to a model property, but just retrieve those values and set your model property programmatically in the controller on postback. 我能想到的最简单的解决方案是不将那些单选按钮映射到模型属性,而只是检索这些值并在回发时在控制器中以编程方式设置模型属性。 You could also look into making IsFelon a nullable property, but using nullable properties in MVC can involve some fiddling around. 您还可以考虑使IsFelon成为可为空的属性,但是在MVC中使用可为空的属性可能会涉及一些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM