C# 数组未正确设置的MVC JSON对象
我正在制作一个工具,在这个工具中,一个人可以获得一个引用,然后将该引用发送给他自己。报价的制作进展顺利,但在发送电子邮件时,报价数据已损坏。C# 数组未正确设置的MVC JSON对象,c#,javascript,jquery,asp.net-mvc,json,C#,Javascript,Jquery,Asp.net Mvc,Json,我正在制作一个工具,在这个工具中,一个人可以获得一个引用,然后将该引用发送给他自己。报价的制作进展顺利,但在发送电子邮件时,报价数据已损坏。 除了选项数组之外,报价单对象中的数据都很好。发送3个数组项目时,选项数组包含3个项目,但其名称为空且价格为0 使用jQuery.post将报价发送到ASP.NET MVC 3 C#中的引号对象如下所示: public class Quotation { public string Email { get; set; } public str
除了
选项
数组之外,报价单
对象中的数据都很好。发送3个数组项目时,选项
数组包含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)
{
//
}
$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
alert(data.Message);
});
{
"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"
}
操作方法如下所示:
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)
{
//
}
$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
alert(data.Message);
});
{
"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"
}
jQuery看起来像:
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)
{
//
}
$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
alert(data.Message);
});
{
"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"
}
jsonContent对象看起来像:
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)
{
//
}
$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
alert(data.Message);
});
{
"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][Name]:额外帕吉纳(52)选项[0][价格]:156
选项[1][名称]:Papier Keuze
选项[1][价格]:68.4
选项[2][Name]:Omslag
期权[2][价格]:41.94
金额:6
底价:149.9189999998
小计:416.258999996
发送价格:0
折扣:45.7884899999999
总价:370.47051
电邮:someone@example.com
这意味着数据确实到达控制器,但选项仍然没有设置正确。我不想事后自己解析它,我想知道正确的处理方法,这样MVC会处理它。如果您想将JSON数据发送到ASP.NET MVC控制器操作,并且希望当前模型绑定工作(例如绑定模型上的集合),则需要将contentType指定为
“应用程序/json”
因为使用$.post
无法指定需要使用$.ajax
的内容类型,还需要JSON。字符串化数据:
$.ajax({
url: baseUrl + "/api/Offerte/",
type: 'POST',
data: JSON.stringify(jsonContent),
contentType: "application/json",
success: function (data) {
alert(data.Message);
}
});
“使用jQuery.post将报价发送到ASP.NET MVC 3。“你为什么有这个限制?您可以改用jQuery.ajax
吗?在Quotence类中,您定义的选项数组包含选项而不是对象。或者您需要重写set函数来解析每个选项对象,并专门设置选项对象的每个属性。@nemesv不是一个限制,“这只是一个选择。”德里克,这似乎是个问题。它们被解析得不好。但是你知道怎么做吗?如果.post()函数是内容类型,那么第四个参数是。对该值使用“json”将work@Derek否第四个参数是:dataType:String服务器所需的数据类型。默认值:智能猜测(xml、json、脚本、文本、html)。
这与contentType不同。@nemesv客户端不是问题所在。问题在于MVC如何解析/读取数据。正如您在我的问题中所看到的,除了数组之外,其他一切都很好。@SynerCoder您试用过我的解决方案吗?我知道只有阵列不工作,但MVC以不同的方式处理集合,这就是为什么其余的都工作了。如果仍然不起作用,您可以尝试使用数据:JSON.stringify(jsonContent),
@nemesv,我只是想确定一下,但不起作用。就像我说的,客户端不是问题所在。这是MVC解析数据的方式。