[英]In ASP.Net Core 5 MVC Controller, when passed a JSON object FromBody that contains a decimal the model is always null
Passing in this json works:传入此 json 有效:
{
"products": [
{
"barcode": "1",
"quantity": 1,
"name": "Barratt Fruit Salad Chews 400 pc box",
"unitPrice": 8,
"totalPrice": 8,
"isInBuyTwoGetOneFreePromotion": false
}
]
}
Passing in this json does not work:传入此 json 不起作用:
{
"products": [
{
"barcode": "8",
"quantity": "4",
"name": "Bonds dinosaurs",
"unitPrice": 0.5,
"totalPrice": 2,
"isInBuyTwoGetOneFreePromotion": true
}
]
}
The reason is the decimal being passed.原因是小数被传递。
My controller has this method我的控制器有这个方法
[HttpPost]
public async Task<JsonResult> UpdateStockAndLogInvoice([FromBody] Invoice invoice)
It references this model:它引用了这个模型:
public partial class Invoice
{
[JsonProperty("products")]
public List<InvoiceItem> Products { get; set; }
}
public partial class InvoiceItem
{
[JsonProperty("barcode")]
public string Barcode { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("quantity")]
public int Quantity { get; set; }
[JsonProperty("totalPrice")]
public long TotalPrice { get; set; }
[JsonProperty("unitPrice")]
public long UnitPrice { get; set; }
[JsonProperty("isInBuyTwoGetOneFreePromotion")]
public bool IsInBuyTwoGetOneFreePromotion { get; set; }
}
I believe the issue is in converting from the float used in javascript to the long used in C#, but I have scoured the internet and cannot work out how to get my model being passed in to not be null.我相信问题在于从 javascript 中使用的浮点数转换为 C# 中长期使用的浮点数,但我已经搜索了互联网并且无法弄清楚如何让我的模型被传入不为空。
Any suggestions gratefully accepted!任何建议感激地接受!
Your failed JSON is not compatible with your InvoiceItem
class in at least two ways:您失败的 JSON 至少在两个方面与您的InvoiceItem
类不兼容:
Quantity
is not a string. Quantity
不是字符串。UnitPrice
is a long, it can't accept floating point values. UnitPrice
是 long,它不能接受浮点值。 During request processing the MVC model binder attempts to deserialize the JSON body into the requested InvoiceItem
type and fails.在请求处理期间,MVC 模型绑定器尝试将 JSON 正文反序列化为请求的InvoiceItem
类型并失败。 It treats this failure as if the body was empty, checks whether you've told it to allow empty bodies (which it does by default) and continues on as if no body had been supplied.它将此失败视为主体为空,检查您是否已告诉它允许空主体(默认情况下它会这样做)并继续像没有提供任何主体一样。
To fix this you need to resolve the difference between your client side and server side models.要解决此问题,您需要解决客户端和服务器端模型之间的差异。 Since JavaScript really doesn't care about types you have to be particularly careful to ensure that you are managing the client side data correctly, otherwise it's not going to deserialize correctly on the server side.由于 JavaScript 真的不关心类型,因此您必须特别小心以确保正确管理客户端数据,否则它不会在服务器端正确反序列化。 Different ASP.NET MVC versions may have different restrictions when it comes to the translations it will handle automatically, but valid JSON that matches your model will always work.不同的 ASP.NET MVC 版本在自动处理的翻译方面可能有不同的限制,但与您的模型匹配的有效 JSON 将始终有效。
So... update your server side model to use decimal for the UnitPrice
and TotalPrice
properties, then fix your client side javascript to put the right value types in.所以...更新您的服务器端模型以对UnitPrice
和TotalPrice
属性使用十进制,然后修复您的客户端 javascript 以放入正确的值类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.