【发布时间】:2012-12-26 22:23:45
【问题描述】:
我正在制作一种工具,在该工具中,人们可以获取报价抛出 javascript,然后将报价发送给自己。报价的制作进展顺利,但在发送电子邮件时,报价数据已损坏。Quotation 对象中的数据很好,除了 Options 数组。当发送 3 个数组项时,Options 数组包含 3 个项,除了它们的名称为空且价格为 0。
报价使用jQuery.post 发送到 ASP.NET MVC 3。
C# 中的引用对象如下所示:
public class Quotation
{
public string Email { get; set; }
public string Product { get; set; }
public int Amount { get; set; }
public decimal BasePrice { get; set; }
public decimal SendPrice { get; set; }
public Option[] Options { get; set; }
public decimal Discount { get; set; }
public decimal SubTotal { get; set; }
public decimal TotalPrice { get; set; }
}
public class Option
{
public string Name { get; set; }
public decimal Price { get; set; }
}
动作方法如下:
[HttpPost]
public JsonResult Offerte(Models.Quotation quotation)
{
//
}
jQuery 看起来像:
$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
alert(data.Message);
});
jsonContent 对象如下所示:
{
"Options":[
{
"Name":"Extra pagina's (16)",
"Price":40
},
{
"Name":"Papier Keuze",
"Price":30
},
{
"Name":"Omslag",
"Price":29.950000000000003
}
],
"Amount":"5",
"BasePrice":99.96000000000001,
"SubTotal":199.91000000000003,
"SendPrice":0,
"Discount":19.991,
"TotalPrice":179.91900000000004,
"Email":"someone@example.com"
}
有人知道为什么数组没有正确设置吗?
编辑
如果我将此调试代码添加到控制器:
using (var writer = System.IO.File.CreateText(Server.MapPath("~/App_Data/debug.txt")))
{
writer.AutoFlush = true;
foreach (var key in Request.Form.AllKeys)
{
writer.WriteLine(key + ": " + Request.Form[key]);
}
}
选项[0][名称]:额外页面的 (52)
选项[0][价格]:156
选项[1][名称]:Papier Keuze
选项[1][价格]:68.4
选项[2][名称]:Omslag
选项[2][价格]:41.94
数量:6
基本价格:149.91899999999998
小计:416.25899999999996
发送价格:0
折扣:45.78848999999999
总价:370.47051
邮箱:someone@example.com
这意味着数据确实到达了控制器,但选项仍然没有正确设置。而且我不想要一个简单的修复,然后我自己解析它,我想知道处理它的正确方法,以便 MVC 处理它。
【问题讨论】:
-
"报价使用 jQuery.post 发送到 ASP.NET MVC 3。"为什么你有这个限制?你能用
jQuery.ajax代替吗? -
看起来在您的 Quotation 类中,您正在定义选项数组来保存选项而不是对象。
-
或者需要重写set函数解析每个option对象,具体设置Option对象的每个属性。
-
@nemesv 不是限制,只是选择。
-
@Derek 这似乎是问题所在。它们的解析不好。但是你知道怎么做吗?
标签: c# javascript jquery asp.net-mvc json