I seem to be having a problem passing a javascript object, which contains an array, to my MVC controller. I have an object which contains two strings, and a string array. The two strings bind correctly, but as soon as I add an array to the obect I get the following error:
Collection is read-only.
Here is my JS + Ajax code:
$('.submit').on('click', function() {
var viewModel = {
FName: "John",
LName: "Doe",
DaysOfTheWeek: ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
};
console.log(viewModel);
$.ajax({
url: "/Home/JsonMethod",
type: "POST",
data: JSON.stringify(viewModel),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log(data);
}
});
});
Here is my MVC controller:
public JsonResult JsonMethod(Person person)
{
return Json(person, JsonRequestBehavior.AllowGet);
}
Here is the Person class:
public class Person
{
public string FName { get; set; }
public string LName { get; set; }
public string[] DaysOfTheWeek { get; set; }
public Person()
{
DaysOfTheWeek = new string[7];
}
}
I've had a look online, but I can't find anything that deals with the following issue. Any help with this matter would be great.