Skip to main content
 首页 » 编程设计

c#之通过 AJAX 传递给 Controller 的列表为空

2024年02月27日38jirigala

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

Controller Action 签名

[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; } 
    } 

和 View

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 和 this answer (仅举几例);如果我使用 JSON.stringify(如某些答案所建议的那样),我会收到 404 响应。我还尝试使用 List 而不是 IEnumerable,使我的 View Model 更小(位置、程序、设备、部件和例程是对于被传回的每个项目都是相同的)并相应地设置 AJAX(返回 404 错误)。如果我设法将其返回到 controller,则 List 为 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:

[对象数组] 0 位置:“ABC123” 程序:“1A2B” 设备类型:“设备” 零件名称:“第 1 部分” RoutineId:“ABC456” FtrName:“功能 1” FtrAttCode:“代码A” 低值:-1.01 高值:1.01 目标值“:空 1

有人能帮忙吗?

请您参考如下方法:

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

您应该通过 POST 发送复杂数据 方法,而不是 GET。 GET 方法适用于发送具有少量属性的小型精益平面 View 模型。这些将由 $.ajax 作为查询字符串发送方法。在您的情况下,您的数据不是平面精益 View 模型。所以你应该使用 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 
} 

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