Reputation: 8628
I'm trying to bind a class object from an endpoint that contains an Enum :-
Sample
public class Person
{
public string Name { get; set; }
public Gender Gender { get; set; }
}
public enum Gender
{
Male = 0,
Female = 1,
}
The Action Signature is as follows :-
public async Task<IHttpActionResult> GetTest([FromBody] Person person)
When the Action method is hit with the folowing object on the body of the request, model binding fails :-
{
"Name": "Derek",
"Gender": "Male"
}
Model binding does work, where i pass the value in via the uri for the enum, but that's not what i want to achieve here.
Do i need to create type converters or is there something simple i'm missing?
Upvotes: 2
Views: 3333
Reputation: 2071
If you are doing a GET request (which I assume you are because of your method name), you cannot read values from the body because there isn't really any data being passed. That's what POST is for. This is why the value works when you pass it in the query string.
Upvotes: 1
Reputation: 9391
Add a property for your enum code :
public int GenderCode{ get; set;}
and pass you enum property in get only :
public Gender Gender{ get { return (Gender)this.GenderCode; } }
And in your view, propose a dropdownlist to present list of possible gender values and assign the user's choice value to the property GenderCode.
Upvotes: 0