所以在 JS 中获取我需要的对象,我做了:
$('.combine-payment-input').each(function (index, value) {
if (parseFloat(value.value) > 0) {
if (methodOfPayment == -1) {
methodOfPayment = value.dataset.method;
}
else {
methodOfPayment = 0;
}
vmopl.push({
id: value.dataset.method,
name: $('label[for="' + value.id + '"]').html(),
inUse: 'True',
ammount: value.value
});
}
});
如果我 console.log
vmopl
最后,我会得到类似的东西
[Object { id="2", name="Card", inUse="True", ammount="500"},
Object { id="1", name="Cash", inUse="True", ammount="250"}]
现在,如果我尝试使用 AJAX 发送它
$.get('/reports/savebill/' + methodOfPayment + '?vmop=' + JSON.stringify(vmopl), function (data) {
if (data == 'True') {
location.href = '/order/neworder/';
} else {
alert("Unsuccessful!");
}
});
Controller 操作应该选择vmop
, Controller 看起来像这样:
public bool SaveBill(int id, ViewMethodOfPayment[] vmop) {
//lots of code...
}
但是当我设置断点时,我总是看到 vmop
为 null,即使我将它传递给另一个对象 (var temp = vmop;
)。
ViewMethodOfPayment
是一个简单的模型类:
public class ViewMethodOfPayment
{
public long Id { get; set; }
public string Name { get; set; }
public bool InUse { get; set; }
public double Ammount { get; set; }
}
如果我遗漏了任何信息,或者不清楚我想做什么/期望做什么,请发表评论,我会尽快回复!
感谢阅读!
编辑:更改了第一 block 代码(第 9 行,因为我包含了一段会带来 JavaScript 错误的代码)
最佳答案
我目前使用的:
Javascript 像您一样通过 JSON.stringify 发送数据。
C#:
public ActionResult AjaxDoSomething(string vmop)
{
var jss = new JavaScriptSerializer();
try
{
var parameter = jss.Deserialize<ViewMethodOfPayment []>(vmop);
//Do something with this data and return the desired result
return Json(result, JsonRequestBehavior.AllowGet);
}
catch
{
return null;
}
}
关于javascript - 通过 AJAX 发送对象数组 - ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245805/