When binding an flag enum type to checkbox lists, only the first checked box gets picked up.
When multiple checkboxes are checked, the bound model property should reflect selections.
The implementation for EnumTypeModelBinder derives from SimpleTypeModelBinder, which only looks at the first value from ValueProviderResult during BindModelAsync(). To fix this issue, line 65 in SimpleTypeModelBinder.cs should be changed from
var value = valueProviderResult.FirstValue;
to
var value = valueProviderResult.ToString();
Thanks for contacting us, @miguelhasse.
It looks like this is a question about how to use ASP.NET Core. While we do our best to look through all the issues filed here, to get a faster response we suggest posting your questions to StackOverflow using the asp.net-core-mvc tag.
@mkArtakMSFT this isn't a question, it's actually a bug report, with a solution! I can send you the sample application I used to test and fix (using ASP.NET Core v2.2).
I have the exact same issue. Only the first value sent is parsed. This is not a question on how to use ASP.NET Core. The title clearly states that there is a specific feature that it is not working as expected.
Thanks for contacting us, @miguelhasse.
Our recommendation in this area is for customers to come up with a custom Model binder, which will do the needful.
This is not something we hear from customers periodically, so we won't spend resources on doing this as there are more important areas to focus on. If, however, you would like to send us a PR with the fix, we'll happily review it.
@mkArtakMSFT I know the solution to the issue, so I'll follow your suggestion and send you a PR with the fix.
A feature like this needs docs, and some kind of design about what the wire-formats we support are. What are the expecations? What does the client send?
Agree, we've already talked with @pranavkm about this. He's going to suggest a design. See https://github.com/aspnet/AspNetCore/pull/14492#issuecomment-536128235
@miguelhasse you might want to add a unit-test that demonstrates what you have fixed.
@huysentruitw get I'll have to do that, as well as add missing tests on EnumTypeModelBinderProviderTest, as there are no actual [Flags] usage scenarios covered!
@pranavkm the wire format is actually described in the implementation of EnumTypeModelBinder.IsDefinedInEnum(), line 65. There are no unit tests covering the implementation for Enum decorated with FlagsAttribute.
Most helpful comment
@mkArtakMSFT this isn't a question, it's actually a bug report, with a solution! I can send you the sample application I used to test and fix (using ASP.NET Core v2.2).