DanilGholtsman
DanilGholtsman

Reputation: 2374

Data sent from ajax as null into the MVC controller

I got JS function which use ajax. It calls the controller which suppose to get data and parse it. It ssems like on client side data variable formed ok bu the controller gets nothing!

I call JS function like getTheDiagramm('/PPTDesign/getReceiversDiagram', recIntervalsRec.total, recIntervalsRec.records);

Here's the function as is

 function getTheDiagramm(action_url, total_d, records_d) {

                        var mainData;

                        var table_form = {
                            total: total_d,
                            records: records_d
                        }
                        var postData = JSON.stringify(table_form);

                        $.ajax({
                            type: "POST",
                            url: action_url,
                            async: false,
                            data: postData,
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                mainData = data;
                            }
                        });

                        return mainData;
                    }

On the controller side

  public JsonResult getReceiversDiagram(DesignReceiversMap postData)
        {
            List<string> resultData = new List<string>();
            return Json(resultData, JsonRequestBehavior.AllowGet);

        }

Models

public class Design {
        public int recid;
        public String well;
        public String q1;   
        public String q2; 
        public String down; 
        public String up; 
    }

    public class DesignReceivers : Design {
        public String period1; 
        public String period2;
    }
    public class DesignReceiversMap {
        public int total;
        public List<DesignReceivers> records;
    }

So, what it sends http://i.gyazo.com/d5ef811e0f32e77f29bb4fe1331f9980.png

and what controller gets http://i.gyazo.com/660bfd8b3808328d974c1c2b89993249.png

I really don't understand what's wrong. Could you help me to fix it, please?

Upvotes: 3

Views: 654

Answers (1)

Your method public JsonResult getReceiversDiagram(DesignReceiversMap postData) expects DesignReceiversMap object as input. But that's not what you send when you call the method.

Try this in your controller:

public JsonResult getReceiversDiagram(string postData)

and this in your js:

$.ajax({
    type: "POST",
    url: action_url,
    async: false,
    data: "postData=" + postData,
    success: function (data) {
        mainData = data;
    }
});

then you can parse the data.

Upvotes: 1

Related Questions