Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to send JSON array to ASP MVC Controller. I first tried a JSON object and it did work but failed while I tried the array JSON object. As the coding showed below, I expected to get the ListForm2 to be,

ListForm2[0].MarketID = 1, ......

ListForm2[1].MarketID = 2, ......

However, I got the ListForm2=null. What's wrong in my coding?

function ProcessSaveView(area, bChecked, bChart, saveName) {
    var jsondata = [{ MarketID: 1, ForecastPointTypeID: 5, ForecastPointID: 21, CustomTimeZoneID: "ET-Prevailing", IsChart: true },
                    { MarketID: 2, ForecastPointTypeID: 5, ForecastPointID: 51, CustomTimeZoneID: "ET-Prevailing", IsChart: true }];

    $.ajax({
        type: "POST",
        url: "./charts/SaveViewToDatabase",
        dataType: "json",
        traditional: true,
        data: jsondata,
        success: function (result) {
            if (result.success) {
                alert("Save View successful!");
            } else {
                alert("Duplicate View already exist. Not save!");
            }
        },
        error: function () {
            alert("No market, load type & region is selected!");
        }
    });
}

[HttpPost]
public ActionResult SaveViewToDatabase(testJsonObject[] ListForm2)
{
    return Json(new { success = true });
}

public class testJsonObject
{
    public int ForecastPointID { get; set; }
    public int MarketID { get; set; }
    public int ForecastPointTypeID { get; set; }
    public string CustomTimeZoneID { get; set; }
    public bool IsChart { get; set; }
}
share|improve this question
    
possible duplicate of Post Array as JSON to MVC Controller – T.S. Jan 2 '15 at 20:53
    
verify your json structure its maybe the mistake – Henrique Yah Centeno Jan 2 '15 at 20:56
    
change the name jsondata for ListForm2={all values} show this forum .. stackoverflow.com/questions/7116099/… – Henrique Yah Centeno Jan 2 '15 at 21:02
up vote 0 down vote accepted

Try the following:

$.ajax({
    type: "POST",
    url: "./charts/SaveViewToDatabase",
    dataType: "json",
    contentType: "application/json",
    data: JSON.stringify(jsondata),
    success: function (result) {
        if (result.success) {
            alert("Save View successful!");
        } else {
            alert("Duplicate View already exist. Not save!");
        }
    },
    error: function () {
        alert("No market, load type & region is selected!");
    }
});

See added contentType and JSON.stringify into data attribute.

share|improve this answer
    
Thanks and it did work! – Michael Lu Jan 2 '15 at 21:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.