1

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>
user3281466
  • 491
  • 1
  • 7
  • 25
  • 2
    Show how you are generating the radio buttons in the view (best guess is you have given the radio button a value of `"null"` instead of `""`). –  Apr 28 '15 at 11:03
  • @StephenMuecke I have edited my post, I can see why it is useful to include this in my OP but at first I omitted it because it was a bit messy as I am using bootstrap styled radios. – user3281466 Apr 28 '15 at 11:11

1 Answers1

1

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

  • This will create a problem later on because when I am saving this to the database I want the values to actually be null and not empty strings. – user3281466 Apr 28 '15 at 11:19
  • It will be `null` when it bound - the `ValueProviders` convert an empty string to null, but cant convert the string `"null"` to `null` - remember your just posting key/value pairs - its all effectively strings –  Apr 28 '15 at 11:21
  • Oh I understand now, I thought the problem is that it wouldn't allow nulls, not because it can't convert "null" to null. Thanks – user3281466 Apr 28 '15 at 11:25
  • And you have a few other odd things in your code such as `@checked = "checked"` - which will be completely ignored so is a bit pointless. The whole purpose of using strongly typed helpers is to bind to your model properties, so its the value of `Continuing` which determines whats selected (not `@checked = "checked"`) - and thankfully because model binding would all fail if didn't :) –  Apr 28 '15 at 11:25
  • Yes, this is strange but it is a workaround to a problem I was having when submitting the form and reloading the page with the same model, the values that were selected to be true would come back as null for some reason. – user3281466 Apr 28 '15 at 11:27
  • http://stackoverflow.com/a/16047172/3281466 - this is the problem I was having, handling the special null case. – user3281466 Apr 28 '15 at 11:29
  • That should not happen, and may disappear now that you change `"null"` to `""`. If not, I suggest to post a new question, showing the view and also the controller. In any case the answer you linked to is utter nonsense and I cant believe it was voted up or accepted. –  Apr 28 '15 at 11:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76452/discussion-between-user3281466-and-stephen-muecke). – user3281466 Apr 28 '15 at 12:07
  • Yes it did disappear after the change. Thanks. – user3281466 Apr 28 '15 at 12:25