时间跨度和"24:00" Asp.net MVC中的解析错误 [英] TimeSpan and "24:00" parse error in Asp.net MVC
问题描述
我的Web应用程序中有一个模式对话框,用户可以在其中输入00:00到24:00之间的时间范围.范围滑块用于选择此范围.
I have a modal dialog in my web application where users are able to enter a time range between 00:00 and 24:00. A range slider is used to select this range.
一切正常,所不同的是,只要用户将正确的范围手柄设置为24:00的值,默认模型联编程序就无法解析此TimeSpan
.
Everything works as expected except that whenever user sets the right range handle to have a value of 24:00 default model binder can't parse this TimeSpan
.
public class Timing
{
public TimeSpan Starts { get; set; }
public TimeSpan Ends { get; set; }
}
我发送回服务器的对象具有IList<Timing>
属性.
My object that gets sent back to server has an IList<Timing>
property.
所以.问题是不能将字符串值"24:00"解析为TimeSpan
实例.是否可以说服默认模型联编程序识别此类字符串值?
So. The problem is just that string value "24:00" can't be parsed to TimeSpan
instance. Is it possible to convince default model binder to recognise such string value?
我想避免将客户端的24:00更改为00:00.我知道我具有Starts
和Ends
属性,但是我的模型验证确认Ends
总是大于Starts
.手动更改为23:59也很麻烦. 基本上可以通过24:00并仍在服务器上进行解析.
I would like to avoid changing 24:00 on the client to 00:00. I know that I have Starts
and Ends
properties but my model validation validates that Ends
is always greater than Starts
. Manual changing to 23:59 is also cumbersome. Basically is it possible to pass 24:00 and still get parsed on the server.
推荐答案
我认为范围过小. 24:00
实际上是第二天的00:00
.
因此它们应该从00:00.00
更改为23:59.99
或其他任何内容.
I think the range is fractionally too large. 24:00
is in fact 00:00
the next day.
so they should go from 00:00.00
to 23:59.99
or whatever.
最终答案(?)将客户端上的24:00
更改为1.0:00
.
这将起作用,因为TimeSpan.Parse("1.0:00").TotalHours
返回24
FINAL ANSWER(?) Change 24:00
on the client to 1.0:00
.
This will work because TimeSpan.Parse("1.0:00").TotalHours
returns 24
:请参阅此处的文档: http: //msdn.microsoft.com/zh-CN/library/se73z7b9.aspx .它显示了天,小时,分钟等的最大范围.根据我的以下评论,小时范围是0
至23
.
See the documentation here: http://msdn.microsoft.com/en-us/library/se73z7b9.aspx. It shows the maximum range for days, hours, mins, etc. For hours it's 0
to 23
as per my comment below.
如果只是让他们选择一个小时的整数,然后在服务器上解析它.
If you are just letting them choose an integer for hours, then parse it on the server.
例如TimeSpan ts = TimeSpan.FromHours(24)
返回1.00:00:00
当然,您总是可以说ts.TotalHours
并且它返回24
.
eg. TimeSpan ts = TimeSpan.FromHours(24)
returns 1.00:00:00
And of course you can always say ts.TotalHours
and it returns 24
.
这篇关于时间跨度和"24:00" Asp.net MVC中的解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!