格式化 MVC 模型 TimeSpan 字段 [英] Formatting MVC model TimeSpan field

查看:5

问题描述

我有一个包含几个时间选择器的 MVC 页面.这些作为可为空的 TimeSpan 存储在模型中的对象列表中.问题是我在输入字段中打印了以秒为单位的时间跨度.有没有办法格式化这些模型属性,以便我以其他方式打印时间跨度(例如 7:00,而不是 07:00:00)?不用说,我对 [DisplayFormat...] 的建议"没有奏效.至少不是我希望的那样.

I have a MVC page containing a few timepickers. These are stored in a list of objects in the model as nullable TimeSpan. The problem is that I get the timespans printed with seconds into the input fields. Is there some way to format these model properties so that I get the timespans printed in some other way (like 7:00, instead of 07:00:00)? Needless to say, my "suggestion" of [DisplayFormat...] didn't work. At least not the way I hoped.

输入字段的定义如下:

@Html.TextBoxFor(x => x.Shifts[i].Start)

模型的相关部分如下所示:

The relevant part of the model looks like:

public class Workshift
{
    [DisplayFormat(Something here perhaps?)]
    public TimeSpan? Start { get; set; }
    public TimeSpan? End { get; set; }
    public TimeSpan? Pause { get; set; }
}

public class TimeRegistrationViewModel
{
    public List<Workshift> Shifts { get; set; }

    ...
}

一如既往地欣赏它!

推荐答案

[DisplayFormat(DataFormatString="{0:hh\:mm}", ApplyFormatInEditMode = true)]
public TimeSpan? Start { get; set; }

[DisplayFormat(DataFormatString="{0:hh\:mm}", ApplyFormatInEditMode = true)]
public TimeSpan? End { get; set; }

[DisplayFormat(DataFormatString="{0:hh\:mm}", ApplyFormatInEditMode = true)]
public TimeSpan? Pause { get; set; }

但请记住,TimeSpan 的主要目的是表示测量的持续时间,而不是一天中的某个时间.这意味着 TimeSpan 值可以超过 24 小时.它们也可以是负数,表示在时间轴上向后移动.

But do keep in mind that the primary purpose for TimeSpan is to represent a measured duration of time, not a time of day. This means that TimeSpan values can be longer than 24 hours. They can also be negative, to represent moving backwards on the timeline.

可接受将它们用作时间,实际上是由框架本身完成的(例如,DateTime.TimeOfDay).但是当以这种方式使用时,您应该仔细验证用户输入.如果您仅依赖数据类型,则用户可能能够输入有效时间跨度的值,但不是有效的白天时间.例如,-1.22:33:44 是一个有效的 TimeSpan,表示过去 1 天 22 小时 33 分 44 秒.

It's acceptable to use them as time-of-day, and is actually done by the framework itself (for example, DateTime.TimeOfDay). But when used this way, you should validate user input carefully. If you just rely on the data type, the user might be able to enter values that are valid time spans, but not valid day times. For example, -1.22:33:44 is a valid TimeSpan that represents 1 day, 22 hours, 33 minutes and 44 seconds in the past.

如果 .Net 有原生的 Time 类型会容易得多,但它没有.更新:现在 CoreFXLab.

It would be so much easier if .Net had a native Time type, but it does not. Update: There is now a native TimeOfDay type in the System.Time package available in CoreFXLab.

另外,TextBoxFor 方法不会获取数据注释.您可以直接将格式字符串指定为参数,如下所示:

Also, the TextBoxFor method will not pick up the data annotation. You can either directly specify the format string as a parameter, like this:

@Html.TextBoxFor(x => x.Shifts[i].Start, "{0:hh\:mm}")

或者你可以像这样切换到 EditorFor:

Or you can switch to EditorFor like this:

@Html.EditorFor(x => x.Shifts[i].Start)
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆