Я пытаюсь передать массив объектов из моего представления в мой контроллер. Однако каждый объект содержит только нулевые значения. Я застрял.Parsing Json массив объекта в массив C# класса в 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" , "BorderColor": "# FFFFFF", "TextColor": "# 7c541b"}}, { "CalendarColor": { "ItemType": "бронирование", "LocationId": "3", "BackgroundColor": "# 464646 " "BorderColor": "# FFFFFF"
...
", "BackgroundColor": "# ff9b99", "BorderColor": "# 000000", "TextColor": "# ff5a56"} }]}
Любые ошибки в консоли браузера? –