1

I have a method in a controller:

    [HttpPost]
    public virtual JsonResult Add(AddItemModel request)

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).

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.

I can't find any differences between our DEV environment and the QA server, but I'm probably not looking at the right thing.

Can anyone suggest what could be causing this difference?

Dev is Win7, QA is Win2003 server, all running latest patches.

Thanks in advance

Griff


Update #1

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.

When the value came in, we picked up the attempted value:

var attValue = modelBindingContext.ValueProvider
    .GetValue(modelBindingContext.ModelName).AttemptedValue;

and then converted this to a decimal value. We also tested what the base's BindModel(controller, context) would return.

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.

However, when the attempted value was '5.1', both our and the MVC code correctly gave us a decimal of 5.1.

So, it appears to us that when:

  • JSON has a value of : 5
  • MVC attempts to bind this to a POCO's property whose datatype is decimal and this fails

but when:

  • JSON has a value of : 5.1
  • MVC attempts to bind this to a POCO's property whose datatype is decimal and this works

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.

Any suggestions here?

3
  • 2
    check for the separating comma, is it , or is it .? maybe the JSON-Serialization switches between cultures and thus corrupts you floating-point number
    – Vogel612
    Commented May 13, 2013 at 10:12
  • Good point. The server (en-US) and the dev machine (en-GB) both use dots and commas in the same way, but I should update the code to explicitly force the locale. In this case, the decimal is just '6' (without quotes). Relevant part of JSON is " ..., 399.99,"ParameterOfInterest":6}]}"
    – DrGriff
    Commented May 13, 2013 at 13:43
  • Added update #1 with some additional info.
    – DrGriff
    Commented May 17, 2013 at 18:12

1 Answer 1

0

See this answer. In short, the default model binder uses .net's type converter, which cannot convert integers to decimals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.