Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 routing corrupting a decimal json value
    text
    copied!<p>I have a method in a controller:</p> <pre><code> [HttpPost] public virtual JsonResult Add(AddItemModel request) </code></pre> <p>This receives an AJAX call. The JSON request sends a serialized version of the "AddItemModel" class and the MVC routing creates the AddItemModel with all the expected properties (which include datatypes such as strings, integers and decimals).</p> <p>I then deploy this to our QA server and the behaviour is different. The JSON request is identical (checked by Fiddler) but the AddItemModel now has all its properties populated EXCEPT the decimal one - this has a value of zero which is the default value.</p> <p>I can't find any differences between our DEV environment and the QA server, but I'm probably not looking at the right thing.</p> <p>Can anyone suggest what could be causing this difference? </p> <p>Dev is Win7, QA is Win2003 server, all running latest patches.</p> <p>Thanks in advance</p> <p>Griff</p> <hr> <p>Update #1</p> <p>We added a custom model binder for both decimal and nullable decimal types and added tracing to this so that we could ascertain what was happening.</p> <p>When the value came in, we picked up the attempted value:</p> <pre><code>var attValue = modelBindingContext.ValueProvider .GetValue(modelBindingContext.ModelName).AttemptedValue; </code></pre> <p>and then converted this to a decimal value. We also tested what the base's BindModel(controller, context) would return.</p> <p>For us, when the attempted value was '5', our conversion gave us a decimal value of 5. But the MVC code couldn't handle this - it returned '' as the object, which then arrives in our controller method as zero.</p> <p>However, when the attempted value was '5.1', both our and the MVC code correctly gave us a decimal of 5.1.</p> <p>So, it appears to us that when:</p> <ul> <li>JSON has a value of : 5</li> <li>MVC attempts to bind this to a POCO's property whose datatype is decimal and this fails</li> </ul> <p>but when:</p> <ul> <li>JSON has a value of : 5.1</li> <li>MVC attempts to bind this to a POCO's property whose datatype is decimal and this works</li> </ul> <p>As I mentioned, this happens only on our QA server, not in DEV. Does this suggest that Microsoft may have already fixed this? But as mentioned, it appears that both our DEV and QA environs are fully patched.</p> <p>Any suggestions here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload