1

I am trying to understand what I think is a model binding problem in my ASP.NET Core project. I have the following 'Index' controller action:

[HttpGet]
public async Task<IActionResult> Index(SortFilterIndexOptions options) { ... }

The SortFilterIndexOptions are four members defined in the following class:

public class SortFilterIndexOptions
{
    public int SelectedBirdId { get; set; }
    public bool ShowAll { get; set; }
    public bool ShowInTable { get; set; }
    public int page { get; set; }
}

These enable the user to filter a paged index page. The bool members are linked to checkbox controls.

I have a fault if the 'ShowAll' bool member is changed to TRUE and then try to navigate to a different page. As the screenshot, below, shows the 'ShowAll' parameter part of the url then contains two bool values ('ShowAll=true, false'):

enter image description here

which of course results in a parse error ('FormatException: String was not recognized as a valid Boolean').

What's happening here? It only happens when the 'ShowAll' parameter is toggled to TRUE. Is it a routing problem because it does not follow the default route pattern? Or is it a problem with the ModelBinder? I am just trying to understand what is going on so I can take the right action. Any help would be appreciated...

Update

This is now issue #3246 ('ModelBinding error with boolean values') on the asp/Home GitHub repository (originally raised by me as issue #1711 on the dotnet/Core repository).

4
  • The error message is pretty clear...ShowAll=true,false is not valid as a boolean. Should work with just ShowAll=true Commented May 16, 2018 at 10:34
  • Is this a checkbox in a form which is sent through GET? Commented May 16, 2018 at 10:41
  • 1
    Yes, I am trying to work out why it adding the two Boolean values. When I click the next page button. This happens the ShowAll is toggled to true. Why is it appending the second ‘false’ value in the url? Commented May 16, 2018 at 10:43
  • @CodeCaster. It is a Bootstrap Toggle control. So the same as a checkbox. In a form to a GET Commented May 16, 2018 at 10:44

2 Answers 2

2

The error message is pretty clear...ShowAll=true,false is not valid as a Boolean.

The model binder is receiving the following string "true,false" from the query string for that parameter and would then try to parse that as a Boolean, which would fail as you have already seen.

Similar to trying

bool value = bool.Parse("true,false");

Should work with just ShowAll=true

Sign up to request clarification or add additional context in comments.

5 Comments

@CodeCaster I am wondering now if the model binder in core has that capability?
Well I know core likes to break a lot of things, but something as fundamental as a checkbox on a form...?
Yes it seems odd. I will change to a drop down list of enumerated values and see if it works. To try to see if the fault is isolated to the checkbox... As I mentioned, one can navigate the different pages of the checkboxes are left as false. It breaks when one is switched to true...
I replaced the checkboxes with drop down lists and the model binding issue was resolved. Which suggests the issue was caused by the ModelBinder with checkboxes. I will leave the question open for a bit to see if anyone can add anything...
0

This issue was investigated by the aspnet/Mvc team under issue #8043.

There is no problem with the ModelBinder. The problem was caused by my pager code, which constructs the url. Please use the link to view the full conversation which contains a detailed explanation from the aspnet/Mvc team and sample code provided by me.

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.