简体   繁体   English

模型联编程序无法处理double.MAX字符串表示形式返回double

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

Right now I have a ViewModel with a property double Maximum . 现在我有一个ViewModel,它的属性为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: 我必须在这行上设置一个断点:

if(ModelState.IsValid)

and check which ModelState property had an error. 并检查哪个ModelState属性有错误。 Then I found that this double Maximum property had an error with the following message: 然后,我发现此double Maximum属性在以下消息中出现错误:

The parameter conversion from type 'System.String' to type 'System.Double' failed. 从“ System.String”类型到“ System.Double”类型的参数转换失败。 See the inner exception for more information. 有关更多信息,请参见内部异常。

On the view side inspecting the HTML with Firebug I can see that the hidden input has this value: 在视图方面,使用Firebug检查HTML ,我可以看到隐藏的输入具有以下值:

1.79769313486232E+308

which correctly represents double.MAX constant. 正确表示double.MAX常数。

I found this Scott Hanselman post from Jan/2005 (almost 9 years ago) which deals with something similar: 我在2005年1月 (差不多9年前)找到了Scott Hanselman的这篇帖子,内容涉及以下内容:

Why you can't Double.Parse(Double.MaxValue.ToString()) or System.OverloadExceptions when using Double.Parse 为什么在使用Double.Parse时无法Double.Parse(Double.MaxValue.ToString())或System.OverloadExceptions

Is there something wrong with my app config or this direct conversion from string back to double is not supported? 我的应用程序配置是否存在问题,或者不支持这种从string直接转换为double直接转换? I think 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... 注意:我尝试用Firebug更改隐藏的输入值,并像Scott在他的帖子中提到的那样:我从最后一位减去1 ...

1.79769313486231E+308

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. 我正在使用@Html.HiddenFor创建隐藏的输入。

After carefully reading Scott's post I saw that he mentions the round-trip specifier. 仔细阅读Scott的帖子后,我发现他提到了往返说明符。 I also found an example here on StackOverflow. 我还在StackOverflow上找到了一个示例

The R stands for "round-trip". R代表“往返”。 From MSDN : MSDN

This format is supported only for the Single and Double types. 仅Single和Double类型支持此格式。 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: 现在,这给了我一个double.MAX字符串表示形式,该表示形式可以在控制器端往返返回为double

1.7976931348623157E+308

Nice... problem solved. 很好...问题解决了。

Hope it helps anyone that might face this same problem in the future. 希望它对将来可能会遇到相同问题的任何人有所帮助。


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 . 值得一提的是,这一切也适用于double.MIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM