简体   繁体   English

在 ASP.Net Core 5 MVC 控制器中,当传递包含小数的 JSON 对象 FromBody 时,模型始终为空

[英]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.所以...更新您的服务器端模型以对UnitPriceTotalPrice属性使用十进制,然后修复您的客户端 javascript 以放入正确的值类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 中的 MVC 控制器的 AJAX POST 数据在传递超过一定长度的对象数组时始终为空 - AJAX POST Data to MVC Controller in ASP.NET Core Always Null When Passing An Object Array Over a Certain Length ASP.NET Core API 中的模型绑定始终为空 - Model binding in ASP.NET Core API are always null 是否可以将文件列表和FormData对象中的整个json对象发送到ASP.NET CORE MVC控制器 - Is it possible to send a list of files and a whole json object in a FormData object to a ASP.NET CORE MVC Controller HttpPost controller 参数 null ASP.NET CORE MVC - HttpPost controller parameters null ASP.NET CORE MVC JSON.stringify时,Json to ASP.NET MVC Controller参数为null - Json to ASP.NET MVC Controller parameter is null when JSON.stringify Ajax 发布到 ASP.net MVC 控制器 - 对象属性为空 - Ajax post to ASP.net MVC controller - object properties are null 在ASP.NET MVC中将模型数据传递给JSON(返回null) - Passing Model Data to JSON in ASP.NET MVC (returning null) ASP.NET MVC:从JavaScript传递到MVC控制器操作后,二维数组为null - ASP.NET MVC : 2d array is null after being passed from javascript to MVC controller action asp.net mvc将Json对象从视图传递到控制器 - asp.net mvc pass Json object from view to controller 如何在 ASP.NET MVC 中将视图模型转换为 JSON 对象? - How to convert View Model into JSON object in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM