Search code examples
c#formsasp.net-mvc-4modelstate

Allow Nullable<bool> in ASP.NET MVC form on submission


I have a model with some properties being of type Nullable.

public Nullable<bool> Continuing { get; set; }
public Nullable<bool> Incomplete { get; set; }

//..etc other properties omitted for brevity

I have successfully bound this in a form with 3 radio buttons but when I submit the form, ModelState.IsValid returns false.

The controller:

        [HttpPost]
        public ActionResult CreateSub(MyModel model)
        {
            var errors = ModelState
                        .Where(x => x.Value.Errors.Count > 0)
                        .Select(x => new { x.Key, x.Value.Errors })
                        .ToArray();

I had a look at the errors and it is saying that the value 'null' is not allowed for the properties of type Nullable<bool> - I don't seem to understand why.

the error:

enter image description here

EDIT:

This is how I am binding the properties to the radios:

        <div class="btn-group" data-toggle="buttons">
                @Html.RadioButtonFor(x => x.Continuing, true, new {@class = "Continuing-true", style="visibility: hidden; margin-left:-13px"})
                <label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && Model.Continuing.Value) { <text>btn-custom-green active</text> } " data-value="true">
                        <input type="radio" name="options" value="true" />
                        T
                </label>

                @if(!Model.Continuing.HasValue) { 
                    @Html.RadioButtonFor(x => x.Continuing, "null", new {@checked = "checked", @class = "Continuing-null", style="visibility: hidden; margin-left:-13px"}) 
                } else {
                    @Html.RadioButtonFor(x => x.Continuing, "null", new {@class = "Continuing-null", style="visibility: hidden; margin-left:-13px"}) 
                }
                <label class="btn-cohort btn btn-sm btn-default btn-n  @if(!Model.Continuing.HasValue) { <text>active</text> } " data-value="null">
                        <input type="radio" name="options" value="null"/>
                        N
                </label>

                @Html.RadioButtonFor(x => x.Continuing, false, new {@class = "Continuing-false", style="visibility: hidden; margin-left:-13px"})
                <label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && !Model.Continuing.Value) { <text>btn-custom-red active</text> } " data-value="false">
                        <input type="radio" name="options" value="false" />
                        F
                </label>
            </div>

Solution

  • Change your Html helper usage to

    @Html.RadioButtonFor(x => x.Continuing, "", ...)
    

    and the manual html to

    <input ... value="" />
    

    so they posts back and empty string as opposed to a string with the value "null" which the ValueProviders cannot convert to null