我試圖從我的視圖傳遞一個對象數組到我的控制器。但是每個對象只包含空值。我卡住了。解析對象的Json數組到MVC3中的類的數組
我的C#類:
public class CalendarColor
{
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string ItemType { get; set; }
public string LocationId { get; set; }
public string TextColor { get; set; }
}
我在控制器方法:
[HttpPost]
public ActionResult SaveColors(IEnumerable<CalendarColor> calendarColors)
{
do the stuff
}
這是JavaScript:
//The Array that will hold the CalendarColorObjects
var calendarColors = [];
//Loop through the table with the settings
$('#tblColorSettings > tbody > tr').each(function() {
var calendarColor = {};
calendarColor.ItemType = $(this).find('td:first').text().trim();
calendarColor.LocationId = $(this).attr('data-location');
calendarColor.BackgroundColor = $(this).find('td:nth(1)').find('input').val();
calendarColor.BorderColor = $(this).find('td:nth(2)').find('input').val();
calendarColor.TextColor = $(this).find('td:nth(3)').find('input').val();
//Add to the Array
calendarColors.push({ CalendarColor : calendarColor });
});
var urlSaveSettings = '/settings/savecolors';
var calendarColorsToBeSaved = { calendarColors: calendarColors };
$.ajax({
type: 'POST',
url: urlSaveSettings,
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify(calendarColorsToBeSaved),
success: function (serverMessage) {
if (serverMessage == "OK") {
alert('OK');
}
},
error: function (msg) {
alert(msg);
}
});
我得到空對象的數組中的我控制器方法。我似乎無法找出什麼是錯的。
JSON的看起來像這樣:
{ 「calendarColors」:[{ 「CalendarColor」:{ 「ItemType的」: 「預訂」, 「LocationId」: 「1」, 「BACKGROUNDCOLOR」: 「#464646」 「邊框顏色」: 「#FFFFFF」, 「TEXTCOLOR」: 「#7c541b」}},{ 「CalendarColor」:{ 「ItemType的」: 「預訂」, 「LocationId」: 「3」, 「BACKGROUNDCOLOR」:「#464646 「 」邊框顏色「: 」#FFFFFF「
...
」, 「BACKGROUNDCOLOR」: 「#ff9b99」, 「邊框顏色」: 「#000000」, 「文字顏色」: 「#ff5a56」} }]}
瀏覽器控制檯中的任何錯誤? – 2013-04-07 18:00:33