EnumTypeModelBinder does not handle enum flags correctly
See original GitHub issueDescribe the bug
When binding an flag enum type to checkbox lists, only the first checked box gets picked up.
Expected behavior
When multiple checkboxes are checked, the bound model property should reflect selections.
Additional context
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();
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Model Bind List of Enum Flags
In general I avoid using enums when designing my view models because they don't play with ASP.NET MVC's helpers and out of the...
Read more >Flags Enum Fields | ChameleonForms
Flags enums have a few rough edges on them if you aren't careful so it's a good ... The default MVC model binder...
Read more >Binding and Validating Enums in ASP.NET Core - Ben Foster
Model binding can throw an exception if trying to bind a non-supported enum name which can be hard to handle gracefully. Our approach...
Read more >C# – Model Bind List of Enum Flags
I have a grid of Enum Flags in which each record is a row of checkboxes to determine that record's flag values. This...
Read more >CA2217: Do not mark enums with FlagsAttribute
Cause. An enumeration is marked with FlagsAttribute and it has one or more values that are not powers of two or a combination...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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).
@mkArtakMSFT I know the solution to the issue, so I’ll follow your suggestion and send you a PR with the fix.