通过 AJAX 传递给控制器​​的列表为空

List passed to controller via AJAX is null

我正在尝试将 objectsList 传回我的 controller,但 Listnull if/when它到达 controller。这是我正在做的事情:

控制器操作签名

[HttpGet]
public ActionResult SaveSpec(IEnumerable<DpvItemLiteVm> alarms){}

查看模型

public class DpvItemLiteVm
    {
        public string Location { get; set; }
        public string Program { get; set; }
        public string DeviceType { get; set; }
        public string PartName { get; set; }
        public string RoutineId { get; set; }
        public string FtrName { get; set; }
        public string FtrAttCode { get; set; }
        public decimal LowVal { get; set; }
        public decimal HiVal { get; set; }
        public decimal? TargetVal { get; set; }
    }

和视图

var alarms = [];

$('#featureSpecGrid tr').each(function () {
     var $tds = $(this).find('td');
     var target = $tds.eq(4).text() === '' ? null : parseFloat($tds.eq(4).text());
     var temp = {
          Location: location,
          Program: program,
          DeviceType: device,
          PartName: part,
          RoutineId: routine,
          FtrName: $tds.eq(0).text(),
          FtrAttCode: $tds.eq(1).text(),
          LowVal: parseFloat($tds.eq(2).text()),
          HiVal: parseFloat($tds.eq(3).text()),
          TargetVal: target
     };
     alarms.push(temp);
});

//alarms = JSON.stringify({ 'alarms': alarms });

//console.log(alarms);

$.ajax({
     type: 'GET',
     cache: false,
     contentType: 'application/json',
     url: '/Dpv/SaveSpec',
     data: alarms
}).done(function (partialViewResult) {
     $('#statusMsg').html(partialViewResult);
}).always(function(result) {
     console.log(result);
});

我试过 this answer, this answer, and this answer(仅举几例);如果我使用 JSON.stringify(如某些答案所建议的那样),我会收到 404 响应。我还尝试使用 List 而不是 IEnumerable,使我的 View Model 更小(位置、程序、设备、部件和例程对于每个被传回的项目都是相同的)和相应地设置 AJAX(returns 一个 404 错误)。如果我设法回到 controllerList 就是 null

如果我 stringify 这是有效负载的示例: {"alarms":"Location":"ABC123","Program":"1A2B","DeviceType":"Device","PartName":"Part1","RoutineId":"ABC456","FtrName":"Feature1","FtrAttCode":"CodeA","LowVal":-1.01,"HiVal":1.01,"TargetVal":null}

如果我不stringify

[object Array] 0 Location:"ABC123" Program:"1A2B" DeviceType:"Device" PartName:"Part1" RoutineId:"ABC456" FtrName:"Feature1" FtrAttCode:"CodeA" LowVal:-1.01 HiVal:1.01 TargetVal":null 1

有人可以帮忙吗?

使用 contentType: 'application/json' 时,您需要将数据作为 JSON 字符串发送,以便模型绑定器能够映射它。

您应该通过 POST 方法发送复杂数据,而不是 GET。 GET 方法适用于发送具有少量属性的小型精益平面视图模型。这些将通过 $.ajax 方法作为查询字符串发送。在您的情况下,您的数据不是平面精益视图模型。因此,您应该使用 POST 作为方法,以便 $.ajax 将在请求正文中发送数据。

我还建议您使用 Url.Action 辅助方法来生成与操作方法相关的正确 url。

var urlToSave="@Url.Action("SaveSpec","Dpv")";
// use urlToSavefor the ajax call 

$.ajax({
     type: 'POST',       
     contentType: 'application/json',
     url: urlToSave,
     data: JSON.stringify(alarms)
}).done(function (partialViewResult) {
     $('#statusMsg').html(partialViewResult);
}).always(function(result) {
     console.log(result);
});

还要确保你的 aciton 方法是 POST

[HttpPost]
public ActionResult SaveSpec(IEnumerable<DpvItemLiteVm> alarms)
{
    // to do : return something
}

现在模型绑定器将能够读取请求正文并将其映射到您拥有的 IEnumerable<DpvItemLiteVm> 参数。