我无法在web api控制器方法(SaveDetails)中获取
JSON数组参数.
这是我的代码.
这是我的代码.
$.ajax( { url : "api/Test/SaveDetails",type : "POST",data : { "employees": [ { "firstName": "John","lastName": "Doe" },{ "firstName": "Anna","lastName": "Smith" },{ "firstName": "Peter","lastName": "Jones" } ] },success: function (data) {alert("success");},error: function () {alert("Error");} })
控制器方法
[HttpPost] public DataSet SaveDetails(Models.Person[] obj) { //save opertion. }
模型方法:
public class Person { public string firstName { get; set; } public string lastName { get; set; } }
解决方法
请尝试以下代码:
声明模型方法如下:
public class Models.employees { public string firstName { get; set; } public string lastName { get; set; } } public class Models.RootObject { public List<employees> employees { get; set; } }
控制器:
[HttpPost] public DataSet SaveDetails([FromBody]RootObject Person) { //save opertion. }
这是预期的结果: