【问题标题】:List passed to controller via AJAX is null通过 AJAX 传递给控制器​​的列表为空
【发布时间】:2017-11-09 18:57:53
【问题描述】:

我正在尝试将objects 中的List 传递回我的controller,但如果/当它到达controller 时,Listnull。这是我正在做的事情:

控制器动作签名

[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 answerthis answerthis answer(仅举几例);如果我使用JSON.stringify(正如一些答案所暗示的那样),我会收到404 响应。我还尝试使用List 而不是IEnumerable,使我的View Model 更小(位置、程序、设备、部件和例程对于传回的每个项目都是相同的)并相应地设置AJAX(返回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

谁能帮忙?

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc asp.net-mvc-4


    【解决方案1】:

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

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

    我还建议您使用Url.Action helper 方法生成正确的相对 url 到 action 方法。

    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&lt;DpvItemLiteVm&gt; 参数。

    【讨论】:

    • 这是我需要的;现在就像一个魅力。很高兴了解POST;我一直使用@using(Html.BeginForm(){} 将大量数据传回控制器进行处理,但我正在添加到现有代码库中,使用form 会更难让它工作。感谢您的帮助!
    猜你喜欢
    • 2020-01-27
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2015-11-22
    • 2019-07-16
    • 2014-02-10
    • 2015-08-13
    相关资源
    最近更新 更多