我有以下.Net类:
public class Product
{
public int ID {get;set;}
public String Name {get;set;}
public Decimal Price {get;set;}
}
我的控制器中有一个动作:
[HttpPost]
public ActionResult AddProduct(Product product)
{
// product.Price is zero!!
}
AddProduct
请求中发布的JSON字符串如下所示(通过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}}
为什么product.Price
为零而其他属性(ID和名称)正确补水?
答案 0 :(得分:2)
尝试发帖: {“产品”:{“ID”:1232,“名称”:“蓝光”,“价格”:210.00}}
我认为MVC不会从int转换/转换为十进制,所以 .00 告诉它使用float / double / decimal保持水合。