通过AJAX - ASP发送一个对象数组.净MVC
本文关键字:一个对象 数组 MVC AJAX ASP 通过 | 更新日期: 2023-09-27 18:10:13
在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!");
}
});
控制器动作应该拾取vmop
,控制器看起来像这样:
public bool SaveBill(int id, ViewMethodOfPayment[] vmop) {
//lots of code...
}
但是当我放置一个断点时,我总是看到vmop
为空,即使我将它传递给另一个对象(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; }
}
如果我错过了任何信息,或者如果不清楚我想做什么/期望什么,请留下评论,我会尽快回答!
感谢阅读!
edit:修改了第一个代码块(第9行,因为我包含了一个会导致JavaScript错误的代码)
我目前使用的:
Javascript通过JSON发送数据。
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;
}
}
尝试将[FromUri]放在ViewMethodOfPayment[] vmop之前
public bool SaveBill(int id,[FromUri] ViewMethodOfPayment[] vmop) {
//lots of code...
}