I'm using ASP.NET Core Razor Pages 7.
A flags enum:
[Flags] // <--- important
public enum Animals { None=0, Dog=1, Cat=2, Rat=4, All=8 }
In a page's code-behind:
[BindProperty] public Animals Animals { get; set; }
In the page's markup:
<input type="checkbox" name="Animals" value="@Animals.Dog" />
<input type="checkbox" name="Animals" value="@Animals.Cat" />
<input type="checkbox" name="Animals" value="@Animals.Rat" />
Problem: when I submit the form, the Animals
property is always bound to the first checked flag, rather than all the flags. For example, if I check "Dog" and "Cat", the property will be bound only to Animals.Dog
.
However I can test that the data is posted correctly, because:
HttpContext.Request.Form["Animals"].ToArray() // = { "Dog", "Cat" }
Where is my mistake? Or must I write a custom model binder for [Flags]
enums? (If so, can you point me to some sample code to get me started quickly.)
This is the
IModelBinder
class I use:Now all you have to do is: Add:
[ModelBinder(BinderType = typeof(EnumModelBinder))]
to theAnimals
enum
difinition.If you have created a
TypeConverter
for the type, that will be used to convert, otherwise standard enum conversion.I supports
<input type="checkbox"
(they must have the same name) and it supports<select multiple
.Here's how to declare the
Animals
enum:(And yo're right: it's surprisingly simple).