0

We are working with the third party vendor who is passing following object as part of Form post

{
    "application": 123,
    "order_reference": "01234",
    "new_status": "initialize"
}

As this is not the standard naming convention in C#, i created following model.

public class Response
{
    [JsonProperty("application")]
    public string Application {get; set;}

    [JsonProperty("order_reference")]
    public string OrderReference {get; set;}

    [JsonProperty("new_status")]
    public string NewStatus {get; set;}
}

And i have below code in controller.

[HttpPost]
public Notify(Response response)
{
   // code.
}

The request is coming to the above method, but the object is not deserializing, I could see that, only application property is getting mapped.

Could any one please let me know, whether i can make it work with the model i have created or do i have to create the same model as the response is coming?

PaRsH
  • 1,320
  • 4
  • 28
  • 56
  • Have you configured the app to use JSON.Net as the default JavaScript serializer? –  May 02 '16 at 07:20
  • @StephenMuecke : no, i have't. Not sure how to do that. And i dont want to make it default for all the Action method. – PaRsH May 02 '16 at 07:21
  • You can use this convention in C# for property names. Just use JASON.Stringify for your data in ajax request. – Aftab Ahmed May 02 '16 at 07:22
  • @AftabAhmed : Could you please elaborate? – PaRsH May 02 '16 at 07:23
  • Have a look at [this question/answer](http://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc). If you don't want to use it as the default, then I think you will need make the parameter a string and use `Json.Net` to deserialize it. –  May 02 '16 at 07:25

1 Answers1

0

Simply wrap your properties within in a json object and stringify it. I have tested it and it's working

   var Response = {
                response:{

                    'Application': "123",
                    'order_reference': "01234",
                    'new_status': "initialize"

                }
            };

C# Code to recieve Ajax Request

    [HttpPost]
    public Notify(Response response)
    {
        // code.
    }

    public class Response
    {
        public string Application { get; set; }

        public string order_reference { get; set; }

        public string new_status { get; set; }
    }
Aftab Ahmed
  • 1,727
  • 11
  • 15