Ich habe die folgende .NET-Klasse:Ist das ein Fehler? Dezimal-Eigenschaft nicht deserialisiert von JSON-String zu werden (.NET MVC)
public class Product
{
public int ID {get;set;}
public String Name {get;set;}
public Decimal Price {get;set;}
}
Und eine Aktion in meinem Controller:
[HttpPost]
public ActionResult AddProduct(Product product)
{
// product.Price is zero!!
}
Der JSON-String in dem entsandte Anfrage an AddProduct
sieht wie folgt aus (griff durch 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}}
Warum ist product.Price
Null, während andere prop erties (ID und Name) werden richtig hydratisiert?
Überprüfen Sie die Request.Form-Auflistung, suchen Sie nach dem Eintrag "Preis" und sehen Sie, was der angegebene Wert ist. –
Siehe http://stackoverflow.com/questions/5698984/default-asp-net-mvc-3-model-binder-doesnt-bind-decimal-propeties. – DMulligan
Diese Sammlung ist leer. Die Anfrage ist nicht URL-codiert, ist JSon-codiert. Ich habe auch HTTP-Header gepostet, das ist klar. – Diego