范围和属性DisplayFormat时间跨度上 [英] Range and DisplayFormat Attributes on TimeSpan

查看:726
本文介绍了范围和属性DisplayFormat时间跨度上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个调度屏需要显示时间字段,为用户输入一天日程安排的时间。

I am building a Scheduling Screen and need to display a Time field for users to enter the time of day for the schedule.

我不知道,如果这是最好的选择,但我使用领域的时间跨度。为了验证输入,我想使用Range属性和DisplayFormat属性。

I'm not sure if this is the best option, but I am using a TimeSpan for the field. To validate the input, I want to use the Range attribute and the DisplayFormat attribute.

当我调试,并进入一个看似有效的值,该范围属性表示超出范围错误。任何人都可以看到我在做什么错了?是时间跨度为这种用法正确的类型? 。任何帮助是极大的赞赏。

When I debug and enter a seeming valid value, the Range attribute indicates an out of range error. Can anyone see what I am doing wrong? Is TimeSpan the proper type for this usage? Any help is greatly appreciated.

模型类:

public class Schedule
{
    public Schedule()
    {
        this.ScheduleTime = new TimeSpan(0, 0, 0);
    }

    /// <summary>
    /// The time of day for the schedule to run
    /// </summary>
    [Required, DataType(System.ComponentModel.DataAnnotations.DataType.Time),
    Display(Name = "Schedule Time", Description = "Number of Hours and Minutes after Midnight Central Timezone"),
    DisplayFormat(DataFormatString = @"{0:hh\:mm\:ss}", ApplyFormatInEditMode = true),
    Range(typeof(TimeSpan), "00:00", "23:59")]
    public TimeSpan ScheduleTime { get; set; }
}



错误消息:

推荐答案

您知道这些时候,你问一个问题,不久后你以前正确的答案只是出现?这是那些对我的一个

You know those times where you ask a question and shortly after the answer just appears right before you? This is one of those for me.

我发现这个职位的SO:
为什么ASP.Net MVC范围属性采取类型?

I found this SO post: why does ASP.Net MVC Range Attribute take a Type?

描述了问题,因为jQuery的暂时无法处理范围表达式所以客户端验证将无法正常工作,但服务器端验证会。

Which describes the issue as jQuery being unable to handle the Range expression so the Client Side Validation won't work, but the Server Side Validation will.

所以我删除与JavaScript本领域的客户端验证:

So I removed the client validation for this field with javascript:

<script>
    $(document).ready(function () {
        $("#ScheduleTime").rules('remove', 'range');
    });
</script>

和现在的验证检查时正常工作的 ModelState.IsValid 在控制器中。

And now the validation works properly when checking the ModelState.IsValid in the controller.

这篇关于范围和属性DisplayFormat时间跨度上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆