I am getting an array through a loop and passing it through an ajax call to my c# method but for some reason I am only getting null values in my parameter but count of the array comes through. Im not sure what I am doing wrong. For example the array before passing it through the ajax would have a count of 3, each key populated with the needed info, but as I pass it through the ajax call and debug on VS, the parameter for my list is null but has a count of 3?
let test = $('.divtest').map(function (index, element) {
return {
key1: $(element).closest('.testdiv')[0].id,
key2: $(element).attr('id'),
key3: $(element).attr('style')
}
}).get();
Ajax Call
$.ajax({
cache: false,
type: "POST",
data: { test: test},
dataType: "json",
url: "/controller/testhere",
success: function (result) {
alert('done');
}
});
C# Method
public JsonResult testhere(List<string> test)
{
//stuff
return null;
}
your action is a list of string but ajax data is not. try this data
let test = [ "key1", "key2", "key3"];
.....
.ajax({
cache: false,
type: "POST",
data: { test: test},
dataType: "json",
url: "/controller/testhere",
success: function (result) {
alert('done');
}
});
or you will have to change the action
Create view model
public class TestViewModel
{
public string Key1 {get; set;}
public string Key2 {get; set;}
public string Key3 {get; set;}
}
action
public JsonResult testhere(TestViewModel test)