2

I have model like this

    public Nullable<bool> Space { get; set; }

    public Nullable<bool> Lab { get; set; }

    public Nullable<bool> Consultation { get; set; }

    public Nullable<bool> Financial { get; set; }

    public Nullable<bool> Other { get; set; } 

in my view i initialize the checkbox as you can see here :

@Html.CheckBoxFor(model => model.Space.Value, new { id = "CheckSpace" })  
@Html.CheckBoxFor(model => model.Lab.Value, new { id = "CheckLab" }) 
@Html.CheckBoxFor(model => model.Consultation.Value, new { id = "CheckConsultation"}) 
@Html.CheckBoxFor(model => model.Financial.Value, new { id = "CheckFinancial" }) 
@Html.CheckBoxFor(model => model.Other.Value, new { id = "CheckOther" }) 

but when i post the view to my controller all checkboxs value are null, could you please give some help?

enter image description here

1
  • 1
    do the checkboxes were checked? Commented Jul 20, 2014 at 10:10

7 Answers 7

3

EDIT

You should make the properties bool rather than bool? to bind them to @Html.CheckBoxFor. The helper only understands the values true or false. Then use

@Html.CheckBoxFor(model => model.Space, new { id = "CheckSpace" })  
@Html.CheckBoxFor(model => model.Lab, new { id = "CheckLab" }) 
@Html.CheckBoxFor(model => model.Consultation, new { id = "CheckConsultation"}) 
@Html.CheckBoxFor(model => model.Financial, new { id = "CheckFinancial" }) 
@Html.CheckBoxFor(model => model.Other, new { id = "CheckOther" }) 

The helper generates both an

<input type="checkbox" name="Model Name" value="true" /> 

and

<input type="hidden" name="Model Name" value="false" /> 

such that a value is sent to the server when the checkbox is not checked.

You may also not want to set a specific id for each checkbox and instead let the framework generate one; You can get the id for a model property using, for example

@Html.IdFor(model => model.Space)
Sign up to request clarification or add additional context in comments.

5 Comments

Can you be more specific? What error messages do you get?
I have to use value ,because if i don't i got convert null to bool error
can't convert expression type <system.nullable<bool> to return type bool
Do you have a custom EditorTemplate for boolean types?
You should make the properties bool rather than bool? to bind them to @Html.CheckBoxFor This seems to be true regardless if you use the Html extensions or manually use an <input type="checkbox">: it just doesn't work if it's nullable.
2

They are posting null because id and name should be same of the element for binding with Model property and you are modifying the id:

Do like this:

@Html.CheckBoxFor(model => model.Space) 

it will be renderd like :

<input type="checkbox" id="Space" name="Space"/>

See that the id and name are the name of your model property if you modify the id the property will not be binded to the model in post and you will get null.

5 Comments

it returns null again
only checked checkboxes value will post
This is not true; id and name do not need to be the same. Only the name of the input is used for binding, not the id, so only the name POSTed needs to correlate with the incoming model type.
i have personally experienced changing id doesn't posts value in model
You can easily test it is not the case by manually creating all inputs only with a name attribute and no ids and posting to a controller action where the name attributes match the setter properties of a model. See the HTML specification for Form Submission - w3.org/TR/html401/interact/forms.html#h-17.13.2
2

@Html.CheckBoxFor() wont work with nullable bools. If you inspect the html that @Html.CheckBoxFor(m =>m.Space.Value) generates you will see something like

<input type="checkbox" name="Space.Value" .....

but you dont have a property named Space.Value, only Space

If you use @Html.EditorFor(), you will see that it generates a select with 3 values (for true, false and null).

2 Comments

Is there a workaround for it? I want to render a checkbox only & not a dropdown, and treat null as false. Changing the model to non nullable is not feasible in my case.
@zeppelin, A checkbox can only have 2 states - checked or unchecked (on/off) which equates to true or false It cannot have a 3rd null state. If your claiming that null and false mean the same thing, then your model should be bool, not (Nullable<bool>) - and use a view model if you do not want to change the data model
1

Check if your check boxes are disabled. If checkboxes are disabled on the view side than it will always send null value back to the model for the disabled property.

Any CSS class setting the checkbox disabled property to true.

Comments

1

You can use this:

@Html.CheckBoxFor(m=>m.Space.Value,new{Name="Space"}),

I hope this will work and the model binding will happen in regular fashion.

Comments

0

It will work:

@Html.CheckBox("Space", Model.Space.HasValue ? Model.Space.Value : false)

Comments

0

About this: Using MVC5, Razor and C#, I done this:

My model is like this:

public partial class SOME_CLASS {
    ...
    public byte COM { get; set; }
    ...
}

And my View in a bool value modeled like Tinyint

<label class="control-label col-lg-3">
    <input type="checkbox" name="COM" id="COM" value="1" />
    Comercial
</label>

The checked status will send the value in value="" attribute

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.