TimeSpan 和“24:00” Asp.net MVC 中的解析错误
我的 Web 应用程序中有一个模式对话框,用户可以在其中输入 00:00 到 24:00 之间的时间范围。范围滑块用于选择该范围。
一切都按预期工作,只是每当用户将正确的范围句柄设置为 24:00 时,默认模型绑定程序就无法解析此 TimeSpan
。
public class Timing
{
public TimeSpan Starts { get; set; }
public TimeSpan Ends { get; set; }
}
我发送回服务器的对象有一个 IList
属性。
所以。问题只是字符串值“24:00”无法解析为 TimeSpan
实例。是否可以说服默认模型绑定器识别这样的字符串值?
我想避免将客户端上的 24:00 更改为 00:00。我知道我有 Starts
和 Ends
属性,但我的模型验证验证 Ends
始终大于 Starts
。手动更改为 23:59 也很麻烦。 基本上有可能经过 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.
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; }
}
My object that gets sent back to server has an IList<Timing>
property.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这个范围有点太大了。
24:00
实际上是第二天的00:00
。所以他们应该从
00:00.00
到23:59.99
或其他。最终答案(?) 将客户端上的
24:00
更改为1.0:00
。这将起作用,因为
TimeSpan.Parse("1.0:00").TotalHours
返回24
编辑: 请参阅此处的文档: http://msdn.microsoft.com/en-us/library/se73z7b9.aspx。它显示天、小时、分钟等的最大范围。根据我下面的评论,小时为
0
到23
。编辑:如果您只是让他们选择一个整数几个小时,然后在服务器上解析它。
例如。
TimeSpan ts = TimeSpan.FromHours(24)
返回
1.00:00:00
当然,您始终可以说
ts.TotalHours
,它会返回24
。I think the range is fractionally too large.
24:00
is in fact00:00
the next day.so they should go from
00:00.00
to23:59.99
or whatever.FINAL ANSWER(?) Change
24:00
on the client to1.0:00
.This will work because
TimeSpan.Parse("1.0:00").TotalHours
returns24
EDIT: 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
to23
as per my comment below.EDIT: If you are just letting them choose an integer for hours, then parse it on the server.
eg.
TimeSpan ts = TimeSpan.FromHours(24)
returns
1.00:00:00
And of course you can always say
ts.TotalHours
and it returns24
.做一个预处理
do a pre-processing
将结束值限制为23:59
24:00==次日00:00
Limit the value of the end to 23:59
24:00 == 00:00 the next day