0

I am posting a json object via ajax

    $('#btnOrder').click(function (e) {
        var jsonData = 
    {
        ScheduleDate: '20/07/2015 17:00',
        UnitNumber: '425196',
        Length: 0.00
    }

        var url = "http://mywebsite.com/Home/MakeOrder";
        $.ajax({
            type: "POST",
            dataType: "json",
            url: url,
            data: JSON.stringify(jsonData),
            contentType: "application/json",
            success: function(result) {

            }
        });
    });

to the following action:

[HttpPost]
public PartialViewResult MakeOrder(string jsonData)
{
    // some actions

    return this.PartialView("_Summary", summaryModel);
}

Model:

public class OrderItem
 {
        public DateTime? ScheduleDate {get;set;}
        public string UnitNumber {get;set;}
        public float Length {get;set;}
    }

The jsonData parameter value always comes null.

When I turn it into get ajax call via

var url = "http://mywebsite.com/Home/MakeOrder/?jsonData=" + jsonData

then the parameter comes fine as json string.

I need to send it via post.

EDIT ***** JS values

I'm debugging in Chrome and jsonData passed to 'ajax data:' is

JSON.stringify(jsonData)

gives

jsonData={"ScheduleDateTime":"20/07/2015 17:00","UnitNumber":"425196","Length":0.00}

if passed non stringified is:

jsonData = Object {"ScheduleDateTime":"20/07/2015 17:00","UnitNumber":"425196","Length":0.00}
nickornotto
  • 1,946
  • 4
  • 36
  • 68
  • have you tried something simple like changing your data element to pass: *{jsonData: JSON.stringify(jsonData)}*, to see if that works correctly? Also have you checked with the browser tools to see what is actually getting sent back to the browser (either with fiddler or network tracing of your browser?) – David Shorthose Jul 20 '15 at 12:15
  • @David I'm debugging in Chrome - see my edit above for the values I get – nickornotto Jul 20 '15 at 12:59
  • So try passing the data as I suggested does that work? – David Shorthose Jul 20 '15 at 16:40
  • @David no it doesn't work. In fact no solution from here. What's strange I've taken 2 examples from internet and applied them exactly as they are in my app and they don't work either. Grabbing Request.Form values in action controller doesn't work either for me. Everything comes as null. Although my colleague did in his local version exactly what I'm doing and it worked for him. So there must be something else which influence my code or it just doesn't like me lol. Anyway I got my colleague's working version so problem solved. Thank you all – nickornotto Jul 21 '15 at 08:19
  • Well glad you got a solution to work in the end. If it was me, it would really annoy me not knowing what caused this issue so would need to find out what the problem was. Well as I said glad you managed to get a solution elsewhere. – David Shorthose Jul 21 '15 at 08:37
  • @David yes sure I wanted to know what the issue is and we were working on the same with my colleague independently. However we need to proceed with the project so it was less time consuming accept his code. However I was also looking to bind the context data directly to the model (eg. MakeOrder(OrderItem model))- without matching json data to each property - but it didn't work for me. I installed Microsoft.AspNet.WebApi nuget package and that seem to do the job now. – nickornotto Jul 22 '15 at 08:08
  • well, nuget webapi package was not needed it turns out so removed it and binding still works as a charm – nickornotto Jul 22 '15 at 09:44

1 Answers1

0

Similar SO question that will solve your issue

You will need to probably create an model to accept a list of instead of a list of strings.

public class MyJSONObject{

    public DateTime ScheduleDate { get; set; }

    public string UnitNumber { get; set; }

    public double Length { get; set; }

}

And accept it in your controller like:

[HttpPost]
public PartialViewResult MakeOrder(List<MyJSONObject> jsonData)
{
    // some actions
    return PartialView("_Summary", summaryModel);
}

And if it's not a list you need then just send off the single object and accept it in your controller as MyJSONObject jsonData instead.

Community
  • 1
  • 1
JDupont
  • 1,352
  • 10
  • 14
  • Updated my answer. Just took a quick google. Please try to be more descriptive with your feedback next time. – JDupont Jul 17 '15 at 17:04
  • Yes, I tried to pass the actual object to the action which in my case is OrderItem (see edit above). In this case the object goes through to the action but its properties don't get bound (they comes null or 0 or empty depends on the type). @JDupont – nickornotto Jul 20 '15 at 10:56
  • In the solution you are linking the array is passed to the action, not a json string – nickornotto Jul 20 '15 at 11:03