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

            @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

How can I bind this check-boxes values to Boolean property fields, properly ?

Answers


There a a 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. Because no value is posted back for Report_Users, its value will always be its default (null) in the POST method. In order to bind to bool? Report_Users then it would need to be CheckBox("Report_User", ...) (underscore not space).

Changing that however means the value of Report_User now always be false instead of null because you have added 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. 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.

But the real issue with your code is that your property is bool? (nullable) and CheckBox() is designed to bind to bool. typeof bool? has 3 states (true/false/null) whereas a checkbox has 2 states (on/off or true/false). 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. In the GET method, when mapping your data model to the view model, you can use

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)
  2. Always use the strongly typed xxxxFor() HtmlHelpers to generate form controls. Not only do you get intellisense, it ensures that the correct name attributes are generated, and any errors are reported
  3. Never attempt to change the value attribute when using the HtmlHelpers (or the name attribute). 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

Need Your Help

PostgreSQL function returning an arbitrary type

sql postgresql plpgsql plv8

Is there any way to have a function in PostgreSQL return an arbitrary type?

How can I tick or untick a checkbox in a datagridview using White

c# .net winforms datagridview white

I have a WinForms app, that displays a DataGridView. It is automatically populated from a BindingSource, and contains several rows of data. The columns include the standard things like strings. ...