简体   繁体   English

布尔模型属性的值未在POST方法中绑定

[英]boolean model propertie's values not bind in POST method

I have table record as follows 我有如下表记录

在此处输入图片说明

this how those two properties defined in model classes 这如何在模型类中定义这两个属性

    public bool? Report_Users { get; set; }
    public bool? Innovation_Discussion_User { get; set; }

this is Form syntax that used to populate Boolean values in checkboxes 这是用于在复选框中填充布尔值的Form语法

            @Html.CheckBox("Report User", Model.Report_Users.GetValueOrDefault(), new { @value = "false" }) Report User
              
            @Html.CheckBox("Innovation Discussion User", Model.Innovation_Discussion_User.GetValueOrDefault(), new { @value = "false" }) Innovation Discussion User

This is check boxes view of that form 这是该表格的复选框视图

在此处输入图片说明

but once I submit , I can see in model those two properties values getting null in POST method 但是一旦提交,我就可以在模型中看到这两个属性值在POST方法中为空

How can I bind this check-boxes values to Boolean property fields, properly ? 如何正确地将此复选框的值绑定到布尔属性字段?

There aa number of issues with you implementation. 实施中存在很多问题。

Your CheckBox("Report User", ...) method is generating a checkbox (and associated hidden input) with name="Report" and your model does not contain a property named Report . 您的CheckBox("Report User", ...)方法正在生成一个name="Report"的复选框(和关联的隐藏输入),并且您的模型不包含名为Report的属性。 Because no value is posted back for Report_Users , its value will always be its default ( null ) in the POST method. 由于没有为Report_Users回发任何值,因此在POST方法中,其值将始终为其默认值( null )。 In order to bind to bool? Report_Users 为了绑定bool? Report_Users bool? Report_Users then it would need to be CheckBox("Report_User", ...) (underscore not space). bool? Report_Users则需要为CheckBox("Report_User", ...) (下划线不是空格)。

Changing that however means the value of Report_User now always be false instead of null because you have added new { @value = "false" } . 但是,更改该值意味着Report_User的值现在始终始终为false而不是null因为您已添加了new { @value = "false" } The CheckBox() method generates 2 inputs, <input type="checkbox".. value="True" /> and <input type="hidden".. value="False" /> in order to correctly bind to your property. CheckBox()方法生成2个输入, <input type="checkbox".. value="True" /><input type="hidden".. value="False" /> ,以便正确绑定到您的属性。 You have now overridden it to generate <input type="checkbox".. value="false" /> so a value of false is posted even if the checkbox is checked. 现在,您已覆盖它以生成<input type="checkbox".. value="false" />因此即使选中了复选框,也会发布false值。

But the real issue with your code is that your property is bool? 但是,您的代码的真正问题在于您的财产是否bool? (nullable) and CheckBox() is designed to bind to bool . (nullable)和CheckBox()旨在绑定到bool typeof bool? bool? has 3 states (true/false/null) whereas a checkbox has 2 states (on/off or true/false). 有3个状态(真/假/空),而复选框有2个状态(开/关或真/假)。 Its not clear why your database property is nullable, but if you cannot change it, then (as always), use a view model with a bool property. 目前尚不清楚为什么您的数据库属性可为空,但是,如果您无法更改它,那么(一如既往)请使用带有bool属性的视图模型。 In the GET method, when mapping your data model to the view model, you can use 在GET方法中,将数据模型映射到视图模型时,可以使用

myViewModel.ReportUser = myDataModel.Report_User.GetValueOrDefault();

and now you can build you view correctly using the strongly typed helper 现在您可以使用强类型帮助器来正确构建视图

@Html.CheckBoxFor(m => m.ReportUser)
@Html.LabelFor(m => m.ReportUser, "Report User") // use a label
// or add [Display(Name="Report User")] and use @Html.LabelFor(m => m.ReportUser)

What to learn from this: 从中学到什么:

  1. If your model is not binding, always check the html your generating (the name attributes of the form controls must match the name of the property) 如果您的模型未绑定,请始终检查生成的html(表单控件的name属性必须与属性名称匹配)
  2. Always use the strongly typed xxxxFor() HtmlHelpers to generate form controls. 始终使用强类型的xxxxFor() HtmlHelpers生成表单控件。 Not only do you get intellisense, it ensures that the correct name attributes are generated, and any errors are reported 您不仅可以智能理解,还可以确保生成正确的name属性,并报告任何错误。
  3. Never attempt to change the value attribute when using the HtmlHelpers (or the name attribute). 使用HtmlHelpers(或name属性)时,切勿尝试更改value属性。 The helpers generate the correct html based on the model property and changing them from the default values will only lead to model binding failing 帮助程序会根据模型属性生成正确的html,并将其更改为默认值只会导致模型绑定失败

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

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