Trying to send data from a JavaScript array to an MVC controller. While debugging, I end up in the method
public void GetJSArray(List<SelectorModel> selectorModels)
However, the selectorModels
is currently showing as null
.
Below is the model being used:
public class SelectorModel
{
public int ID { get; set; }
public string Cords { get; set; }
public string Name { get; set; }
}
Here is the C# code in the controller:
[HttpPost]
public void GetJSArray(List<SelectorModel> selectorModels)
{
//Code here
}
And this is the JavaScript/Ajax function:
function SaveInfoSpots() {
var savedeSpots = JSON.stringify({ selectorModels: infospots });
$.ajax({
type: "POST",
url: '/Home/GetJSArray',
contentType: "application/json; charset=utf-8",
dataType: 'JSON',//json
data: savedeSpots,
traditional: true
});
}
When making a console.log(savedeSpots)
, the following is displayed:
{"selectorModels":[{"ID":1,"Name":"InfoSpot1","Cords":"5000.00, 2293.85, 2278.05"},{"ID":1,"Name":"InfoSpot2","Cords":"1.94, 584.50, 5000.00"},{"ID":1,"Name":"InfoSpot3","Cords":"5000.00, -2705.97, -277.02"},{"ID":1,"Name":"InfoSpot4","Cords":"5000.00, 504.93, -2845.93"}]}