【发布时间】:2017-11-09 18:57:53
【问题描述】:
我正在尝试将objects 中的List 传递回我的controller,但如果/当它到达controller 时,List 是null。这是我正在做的事情:
控制器动作签名
[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 和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:
[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