Reputation: 18359
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 an action in my controller:
[HttpPost]
public ActionResult AddProduct(Product product)
{
// product.Price is zero!!
}
The JSON string posted in the request to AddProduct
looks like this (grabbed through 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) get hydrated correctly?
Upvotes: 5
Views: 2027
Reputation: 3455
Try posting: {"Product":{"ID":1232, "Name":"Blu-Ray","Price":210.00}}
I think MVC doesn't cast/convert from int to decimal so the .00 tells it to hydrate using float/double/decimal.
Upvotes: 2