Nem*_*jaT 7 javascript c# ajax asp.net-mvc jquery
所以在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
});
}
});
Run Code Online (Sandbox Code Playgroud)
如果我console.log vmopl最终,我会得到类似的东西
[Object { id="2", name="Card", inUse="True", ammount="500"},
Object { id="1", name="Cash", inUse="True", ammount="250"}]
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试将此发送给AJAX,请使用
$.get('/reports/savebill/' + methodOfPayment + '?vmop=' + JSON.stringify(vmopl), function (data) {
if (data == 'True') {
location.href = '/order/neworder/';
} else {
alert("Unsuccessful!");
}
});
Run Code Online (Sandbox Code Playgroud)
控制器动作应该拿起vmop,控制器看起来像这样:
public bool SaveBill(int id, ViewMethodOfPayment[] vmop) {
//lots of code...
}
Run Code Online (Sandbox Code Playgroud)
但是当我放置一个断点时,我总是看到vmopnull,即使我将它传递给另一个对象(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; }
}
Run Code Online (Sandbox Code Playgroud)
如果我错过了任何信息,或者我不清楚我想做什么/期望什么,请发表评论,我会尽快回答!
谢谢阅读!
编辑:更改了第一个代码块(第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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
969 次 |
| 最近记录: |