question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EnumTypeModelBinder does not handle enum flags correctly

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
miguelhassecommented, May 13, 2019

@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).

1reaction
miguelhassecommented, Sep 27, 2019

@mkArtakMSFT I know the solution to the issue, so I’ll follow your suggestion and send you a PR with the fix.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found