Reputation: 1200
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'):
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).
Upvotes: 1
Views: 1409
Reputation: 1200
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.
Upvotes: 0
Reputation: 247018
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
Upvotes: 2