But are you making sure to POST the data back over to the Product POST method?
The Post method gets hit, put a breakpoint there, and if the Price is set to for example 23 then it gets passed in correctly. As soon as do something like 23.45 then it becomes 0. In the mean time I also added a Name property of type string to see if that
gets passed and that does. I also updated my Post method to the following:
public Product Post(Product product)
{
product.Price += 13.45m;
product.Name += " and I want mayo with that!";
return product;
}
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Yes, works for me. So might just be something silly that's being overlooked.
I would for sure expect it to work here too as it's a rather trivial example to toy with. If you can zip up your solution and put it on a public skydrive or so I'm happy to test it out in VS2010 (which I made use of during the test).
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
I would for sure expect it to work here too as it's a rather trivial example to toy with. If you can zip up your solution and put it on a public skydrive or so I'm happy to test it out in VS2010 (which I made use of during the test).
However if I only transmit 23, so without the .45, then it also works.
I think one of the reason that you are seeing this behaviour is because of current culture. Every one can easily confirm the behaviour that Kris is gettting by adding this code in global.asax.
protected void Application_BeginRequest()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-PT");
}
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
It for sure has something to do with the culture and UIculture settings all right. Normally I'm on nl=BE and when I switch to en-US on my pc then it works out fine. When added this to web.config:
protected void Application_BeginRequest()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE");
}
But still no go.
Update: and then I got awake after eating my breakfast and then rethought the issue and came to the conclusion that I did it wrong and had to input 23,45 now instead with the nl-BE settings and then it works out nicely. When setting all
the culture stuff to en-US and I'm a happy camper again and can now see the 23.45 coming in. Not the first time cultural differences have made people pull their hair (and sometimes even cause wars unfortunately).
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
I think part of the problem you're having is that you are also encoding the decimal value as a string not as a JSON number literal:
{
"ID":"13",
"Price":"23.45"
}
The price shouldn't be in quotes. I think if you had the value as a JSON literal this would actually work properly as JSON literals are culture neutral.
I'm betting that having any value in quotes as a string, trying to parse it to a non-string value likely causes .NET (or Json.net) to .Parse() in the current culture which can have wrong behavior.
XIII
All-Star
182707 Points
23464 Posts
ASPInsiders
Moderator
MVP
Modelbinding problem
Apr 04, 2012 08:54 PM|LINK
Hi,
I've got the following model:
public class Product { public int ID { get; set; } public decimal Price { get; set; } }and the following Get method in my apicontroller which works perfectly fine when being called from Fiddler:
public IQueryable<Product> Get() { var products = new List<Product> { new Product() {ID = 1, Price = 3.45m}, new Product() {ID = 2, Price = 5.67m}, new Product() {ID = 3, Price = 17.00m}, new Product() {ID = 4, Price = 23.67m} }; return products.AsQueryable(); }For the Post I've got the following method:
public Product Post(Product product) { product.Price += 13; return product; }but when I try to call it from fiddler with the following:
Content-Type:application/json
and in the body:
{ "ID":"13", "Price":"23.45" }only the ID gets through. The Price becomes 0 for some reason. I'm likely looking over something trivial here but could use a pointer to investigate.
However if I only transmit 23, so without the .45, then it also works.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Datawalke
Member
212 Points
27 Posts
Re: Modelbinding problem
Apr 04, 2012 09:06 PM|LINK
Hi XIII,
This may sound a little foolish: But are you making sure to POST the data back over to the Product POST method?
http://datawalke.com
BrockAllen
All-Star
27554 Points
4912 Posts
MVP
Re: Modelbinding problem
Apr 04, 2012 09:07 PM|LINK
Yes, works for me. So might just be something silly that's being overlooked.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
XIII
All-Star
182707 Points
23464 Posts
ASPInsiders
Moderator
MVP
Re: Modelbinding problem
Apr 04, 2012 09:16 PM|LINK
Hi,
The Post method gets hit, put a breakpoint there, and if the Price is set to for example 23 then it gets passed in correctly. As soon as do something like 23.45 then it becomes 0. In the mean time I also added a Name property of type string to see if that gets passed and that does. I also updated my Post method to the following:
public Product Post(Product product) { product.Price += 13.45m; product.Name += " and I want mayo with that!"; return product; }Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
XIII
All-Star
182707 Points
23464 Posts
ASPInsiders
Moderator
MVP
Re: Modelbinding problem
Apr 04, 2012 09:21 PM|LINK
Hi,
I would for sure expect it to work here too as it's a rather trivial example to toy with. If you can zip up your solution and put it on a public skydrive or so I'm happy to test it out in VS2010 (which I made use of during the test).
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
BrockAllen
All-Star
27554 Points
4912 Posts
MVP
Re: Modelbinding problem
Apr 04, 2012 09:38 PM|LINK
Sent in PM.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Modelbinding problem
Apr 05, 2012 03:41 AM|LINK
I think one of the reason that you are seeing this behaviour is because of current culture. Every one can easily confirm the behaviour that Kris is gettting by adding this code in global.asax.
protected void Application_BeginRequest() { System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-PT"); }Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
XIII
All-Star
182707 Points
23464 Posts
ASPInsiders
Moderator
MVP
Re: Modelbinding problem
Apr 05, 2012 04:03 AM|LINK
Hi,
It for sure has something to do with the culture and UIculture settings all right. Normally I'm on nl=BE and when I switch to en-US on my pc then it works out fine. When added this to web.config:
and this in the global.asax:
protected void Application_BeginRequest() { System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE"); System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE"); }But still no go.
Update: and then I got awake after eating my breakfast and then rethought the issue and came to the conclusion that I did it wrong and had to input 23,45 now instead with the nl-BE settings and then it works out nicely. When setting all the culture stuff to en-US and I'm a happy camper again and can now see the 23.45 coming in. Not the first time cultural differences have made people pull their hair (and sometimes even cause wars unfortunately).
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Modelbinding problem
Apr 05, 2012 04:39 AM|LINK
Use comma for point for this culture,
{
"ID":"13",
"Price":"23,45"
}
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
rstrahl
Contributor
2233 Points
375 Posts
ASPInsiders
MVP
Re: Modelbinding problem
Apr 05, 2012 05:11 AM|LINK
Kris -
I think part of the problem you're having is that you are also encoding the decimal value as a string not as a JSON number literal:
{ "ID":"13", "Price":"23.45" }The price shouldn't be in quotes. I think if you had the value as a JSON literal this would actually work properly as JSON literals are culture neutral.
I'm betting that having any value in quotes as a string, trying to parse it to a non-string value likely causes .NET (or Json.net) to .Parse() in the current culture which can have wrong behavior.
+++ Rick ---
West Wind Technologies
Making waves on the Web
www.west-wind.com/weblog