2015-01-02 5 views
0

Я пытаюсь отправить массив JSON в ASP MVC Controller. Сначала я попробовал объект JSON, и он работал, но не удался, когда я попробовал объект JSON массива. По мере того как кодирование показано ниже, я ожидал, чтобы получить ListForm2 быть,Как отправить JSON-массив в ASP.NET MVC-контроллер

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

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

Однако я получил ListForm2 = null. Что не так в моей кодировке?

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; } 
} 
+0

Возможный дубликат [Post Array as JSON to MVC Controller] (http://stackoverflow.com/questions/10007722/post-array-as-json-to-mvc-controller) –

+0

проверить свою структуру json, возможно, ошибка –

+0

изменить имя jsondata для ListForm2 = {все значения} показать этот форум .. http://stackoverflow.com/questions/7116099/send-array-to-mvc-controller-via-json –

ответ

1

Попробуйте следующее:

$.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!"); 
    } 
}); 

См добавил contentType и JSON.stringify в data атрибута.

+0

Спасибо, и это действительно сработало! –

Смежные вопросы