Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Asp.NET核心模型绑定输入类型复选框未保持未选中状态_Asp.net Core_Razor_Checkbox - Fatal编程技术网

Asp.net core Asp.NET核心模型绑定输入类型复选框未保持未选中状态

Asp.net core Asp.NET核心模型绑定输入类型复选框未保持未选中状态,asp.net-core,razor,checkbox,Asp.net Core,Razor,Checkbox,我面临着一些奇怪的问题,我正在使用Asp.net内核 我有一个绑定到razor视图的模型类,下面是模型实现 { PatientDetailReport = new ReportModel(); itemid = true; } public ReportModel PatientDetailReport { get; set; } public bool itemid { get; set; } 报

我面临着一些奇怪的问题,我正在使用Asp.net内核

我有一个绑定到razor视图的模型类,下面是模型实现

        {
            PatientDetailReport = new ReportModel();
            itemid = true;
        }
public ReportModel PatientDetailReport { get; set; }
        public bool itemid { get; set; }
报表模型类几乎没有bool属性,例如
public bool IdentityNumberDisplay{get;set;}

我正试图以这两种方式绑定模型,正如大多数博客和stackoverflow上提到的

      (1) <input type="checkbox"  asp-for="@Model.PatientDetailReport.IdentityNumberDisplay"   />
     (2)  @Html.CheckBoxFor(m=>m.PatientDetailReport.IdentityNumberDisplay,new { })
    </td>
(1)
(2) @Html.CheckBoxFor(m=>m.PatientDetailReport.IdentityNumber显示,新{})
但这两种情况都没有得到遏制

对于第一种情况,我还尝试了
value=@Model.PatientDetailReport.IdentityNumber显示
但在jquery级别,我必须使用value=True或False(作为字符串)来检查它,我可以修改复选框,但该值没有发布到控制器上

关于这个案子,有谁能给我指点一下吗。 为什么案例2不起作用,但是大多数博客都说这样使用


谢谢

我可以重现您的问题,请确保在您的情况下,
PatientDetailReport.IdentityNumber Display
已设置为
true

因为您的代码没有为其赋值,而默认值
false
会导致复选框未选中

您可以如下所示设置默认值以进行测试:

public bool IdentityNumberDisplay{get;set;}=true

<form method="post">
    @Model.PatientDetailReport.IdentityNumberDisplay
    @*see the value of IdentityNumberDisplay*@

    <input type="checkbox" asp-for="@Model.PatientDetailReport.IdentityNumberDisplay" />
    @Html.CheckBoxFor(m=>m.PatientDetailReport.IdentityNumberDisplay,new { })

    <input type="submit" value="submit" />
</form>

@Model.PatientDetailReport.IdentityNumber显示
@*请参见IdentityNumberDisplay的值*@
@CheckBoxFor(m=>m.PatientDetailReport.IdentityNumber显示,新{})

我使用JQuery解决了这个问题,方法是选中模型中的复选框值,该值以字符串格式显示为“True”,然后将checked属性指定给True

 @Html.CheckBoxFor(m=>m.PatientDetailReport.IdentityNumberDisplay
   ,new {value=Model.PatientDetailReport.IdentityNumberDisplay })

我不确定它是否正确,但这解决了我在asp.net core模型的bool属性(复选框或单选按钮)中遇到的问题。

很抱歉,此问题的标题出现错误,是“复选框保持未选中状态”。我已选中此代码,并在调试模式下运行此代码,该属性的值为true。事实上,出于测试目的,我在构造函数级别设置itemid=true,并且它也显示为未选中。
 $.each($("input[type='checkbox']"), function (e) {
                console.log($(this).val());
                //debugger;
                if ($(this).val() === 'True') {
                    $(this).prop('checked', true);
                }

            })