Model binder can't handle double.MAX string representation back to double


Right now I have a ViewModel with a property double Maximum. On the view side it's kept in a hidden input to help with unobtrusive validation.

When post backing the values, the binding silently fails. I had to put a breakpoint on this line:


and check which ModelState property had an error. Then I found that this double Maximum property had an error with the following message:


The parameter conversion from type 'System.String' to type 'System.Double' failed. See the inner exception for more information.


On the view side inspecting the HTML with <a href="https://getfirebug.com/" rel="nofollow">Firebug</a> I can see that the hidden input has this value:


which correctly represents <a href="http://msdn.microsoft.com/en-us/library/system.double.maxvalue.aspx" rel="nofollow">double.MAX</a> constant.

I found this Scott Hanselman post from <em>Jan/2005</em> (almost 9 years ago) which deals with something similar:

<a href="http://www.hanselman.com/blog/WhyYouCantDoubleParseDoubleMaxValueToStringOrSystemOverloadExceptionsWhenUsingDoubleParse.aspx" rel="nofollow">Why you can't Double.Parse(Double.MaxValue.ToString()) or System.OverloadExceptions when using Double.Parse</a>

Is there something wrong with my app config or this direct conversion from string back to double is not supported? I <em>think</em> it should handle it without errors.

Note: I tried changing the hidden input value with Firebug and did as Scott mentions on his post: I subtracted 1 from the last digit...


and did a postback again just to find the model binder handled it correctly this time.


I'm using @Html.HiddenFor to create the hidden input.

After carefully reading Scott's post I saw that he mentions the <em>round-trip</em> specifier. I also found <a href="https://stackoverflow.com/a/611564/114029" rel="nofollow">an example</a> here on StackOverflow.


The R stands for "round-trip". From <a href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx?ppud=4" rel="nofollow">MSDN</a>:

This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value.


So I did this:

@Html.HiddenFor(m => m.Maximum, new { Value = Model.Maximum.ToString("R") })

Now this gives me a double.MAX string representation that can be round-tripped back to a double on the controller side:


Nice... problem solved.

Hope it helps anyone that might face this same problem in the future.

<hr />

How interesting this is?!

1.79769313486232E+308 // double.MAX 1.7976931348623157E+308 // double.MAX.ToString("R")

It's worth mentioning that all this is also applicable to double.MIN.


  • How to Detect if an XML Element is Empty using DOMDocument in PHP?
  • urllib2 returns a different page the browser does?
  • Fonts changing weight when uploaded to web server
  • jQuery not recognizing Symfony JSON?
  • How to throttle concurrent Async webrequests
  • Equivalent for np.add.at in tensorflow
  • EventLog logs in Application, even though set to another log
  • add new field to form with rvest
  • getElementsByClassName and AJAX
  • Lua string.gsub() by '%s' or '\\n' pattern
  • get iframe content as string
  • Multicolored edittext hint
  • RxJava debounce by arbitrary value
  • Create Instant using a negative year
  • Redux Form - Not able to type anything in input
  • D3 get axis values on zoom event
  • C: Incompatible pointer type initializing
  • How can I sort a a table with VBA with given text condition?
  • Use of this Javascript
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Linq Objects Group By & Sum
  • Spring Data JPA custom method causing PropertyReferenceException
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • How to get next/previous record number?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • How to get icons for entities from eclipse?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • apache spark aggregate function using min value
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++