1

I have an Asp.NET MVC controller action which receives a DTO which in turn contains a nullable decimal property.

e.g.

 [DataContact]
 public class MyDTO
 {
     //other properties

     [DataMember]
     public decimal? SomeProp {get;set;}
 }

and my action looks like this:

 public JsonResult SaveMyDTO(MyDTO dto)
 {...

I can see that the js client passes the correct json data, there are values set for "SomeProp" however, the property is not set when deserialized on the server, other properties are, but not the nullable decimal prop.

What is the easiest way to make it work? convert the property to string?

2 Answers 2

1

I assume you are sending the JSON data to SomeProp as a numerical value, something like this:

var json = { "SomeProp": 2.233 };

The serialisation routine in MVC sometimes has issues with nullable numerical values, so if that is the case, try converting that value to a string before sending the request:

var json = { "SomeProp": "2.233" };

edit
Just noticed you mentioned this solution in the last line of your question :)

Sign up to request clarification or add additional context in comments.

Comments

1

Changing the property to nullable double makes it work correctly.

3 Comments

sorry but no it doesn't, the binder chokes on any nullable properties.
there might be some more funniness going on then. I tested it in total isolation with a single controller action taking a single parameter of a type with a single property. double? doesn't work and double works in that scenario. When passed in as "name":"0.0" they both work fine.
I think the reason it's working is probably because you're using MVC4 or higher. I'm still stuck on MVC3 and it doesn't work.

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.