2

I have a problem with ajax while sending complex key-value pairs from view to controller. I wrote my ajax queries. Except "currentStateDatas" all other variables are passing correctly to controller. But, currentStateDatas comes null from view. Controller waits "string" for "currentStateDatas" because I need string type for it. I have could not figure out the problem. Could you please help me ? By the way "currentStateDatas" contains json. It has too many nested key value pairs.

currentStateDatas = {};
//some values are taken from forms.
$.ajax({
        url: "@Url.Action("SaveTempReport", "Report")",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ "categoryIds": categoryIds, "reportName": reportName, "Description": description, "tempReportId": tempReportId, "chartState": currentStateDatas, "deparmentIds": deparmentIds}),
        success: function (response) {

        }
    });
1
  • It would be awesome if you could provide a minimal reproducible example, so we can see how categoryIds is populated etc. Commented Jan 16, 2019 at 10:48

1 Answer 1

5

Your controller waits for currentStateDatas as a string, but in your js code it's an object, you should also convert currentStateDatas to string by using JSON.stringify.

$.ajax({
        url: "@Url.Action("SaveTempReport", "Report")",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ "categoryIds": categoryIds, 
                               "reportName": reportName, 
                               "Description": description, 
                               "tempReportId": tempReportId, 
                               "chartState": JSON.stringify(currentStateDatas), 
                               "deparmentIds": deparmentIds}),
        success: function (response) {
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Hi mate, it works perfectly :) thank you so much , I appreciate it :))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.