2

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?

1
  • Have you tried "Gender": 0? Commented Feb 12, 2016 at 9:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Id come to realise this, school boy error. I wast testing this out and realised the error.. Duh
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.