This is mistake? A decimal property does not receive deserialization from a JSON string (.NET MVC)

I have the following .Net class:

public class Product { public int ID {get;set;} public String Name {get;set;} public Decimal Price {get;set;} } 

And the action in my controller:

 [HttpPost] public ActionResult AddProduct(Product product) { // product.Price is zero!! } 

The JSON string sent in the AddProduct request looks like this (captured via Fiddler2):

 POST http://localhost:59656/Cart/AddProduct HTTP/1.1 Host: localhost:59656 Origin: http://localhost:59656 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko Chrome/17.0.963.79 Safari/535.11 Content-Type: application/json; charset=UTF-8 Accept: text/html, */*; q=0.01 {"Product":{"ID":1232, "Name":"Blu-Ray","Price":210}} 

Why is product.Price zero, while other properties (ID and Name) are properly moistened?

+4
source share
1 answer

Try sending: {"Product": {"ID": 1232, "Name": "Blu-Ray", "Price": 210.00}}

I think MVC does not convert / convert from int to decimal, so .00 tells it to hydrate using float / double / decimal.

+2
source

Source: https://habr.com/ru/post/1403993/


All Articles