我通过循环获得一个数组,并通过ajax调用传递给我的c#方法,但由于某种原因,我只在参数中获得空值,但数组的计数通过。我不确定我做错了什么。例如,在通过ajax传递数组之前,数组的计数为3,每个键填充所需的信息,但当我通过ajax调用传递它并在VS上调试时,我的列表的参数为空,但计数为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调用
$.ajax({
cache: false,
type: "POST",
data: { test: test},
dataType: "json",
url: "/controller/testhere",
success: function (result) {
alert('done');
}
});
c#方法public JsonResult testhere(List<string> test)
{
//stuff
return null;
}
您的操作是字符串列表,但ajax数据不是。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');
}
});
否则你将不得不更改
创建视图模型
public class TestViewModel
{
public string Key1 {get; set;}
public string Key2 {get; set;}
public string Key3 {get; set;}
}
行动public JsonResult testhere(TestViewModel test)