0

I have a case where the JavaScript client side model does not match with the WebAPI model. In the following code, the status on the client side is a string while the same in the WebAPI is an integer.

How can I achieve the mapping of status from string to an integer without adding an if statement in the Post method and without modifying the Order model?

$.post('/Shipping/UpdateStatus', { orderId: 1000, status: "Accepted"} )

void Post([FromBody] Order order)
{
}

class Order
{
    int OderId { get; set; }
    int ShippingStatus { get; set; };
}
wonderful world
  • 10,969
  • 20
  • 97
  • 194

1 Answers1

3

You can use a custom JSON.NET converter to achieve this. See this post for detailed information. For more information regarding this topic see Parameter Binding in ASP.NET Web API

class Order
{
    int OderId { get; set; }
    [JsonConverter(typeof(CustomIntConverter))]
    int ShippingStatus { get; set; };
}
Community
  • 1
  • 1
Luuk Moret
  • 506
  • 1
  • 6
  • 16