asp.net-mvc 格式化 MVC 模型 TimeSpan 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17571291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting MVC model TimeSpan field
提问by SamiHuutoniemi
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.
我有一个包含几个时间选择器的 MVC 页面。它们作为可空存储在模型中的对象列表中TimeSpan。问题是我在输入字段中打印了以秒为单位的时间跨度。有什么方法可以格式化这些模型属性,以便我以其他方式打印时间跨度(例如7:00, 而不是07:00:00)?不用说,我的“建议”[DisplayFormat...]没有奏效。至少不是我希望的方式。
The input fields are defined like this:
输入字段定义如下:
@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; }
...
}
Appreciate it as always!
一如既往的欣赏!
回答by Matt Johnson-Pint
[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; }
But do keep in mind that the primary purpose for TimeSpanis to represent a measured duration of time, not a time of day. This means that TimeSpanvalues can be longer than 24 hours. They can also be negative, to represent moving backwards on the timeline.
但请记住,主要目的TimeSpan是表示测量的持续时间,而不是一天中的时间。这意味着TimeSpan值可以长于 24 小时。它们也可以是负数,表示在时间轴上向后移动。
It's acceptableto 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:44is a valid TimeSpanthat represents 1 day, 22 hours, 33 minutes and 44 seconds in the past.
将它们用作一天中的时间是可以接受的,并且实际上是由框架本身完成的(例如,DateTime.TimeOfDay)。但是当以这种方式使用时,您应该仔细验证用户输入。如果您仅依赖于数据类型,用户可能能够输入有效时间跨度的值,但不是有效的日时间。例如,-1.22:33:44是TimeSpan表示过去1 天 22 小时 33 分 44 秒的有效值。
It would be so much easier if .Net had a native Timetype, but it does not. Update: There is now a native TimeOfDaytype in the System.Timepackage available in CoreFXLab.
如果 .Net 有本机Time类型会容易得多,但它没有。 更新:CoreFXLabTimeOfDay中的System.Time包中现在有一个本机类型。
Also, the TextBoxFormethod will not pick up the data annotation. You can either directly specify the format string as a parameter, like this:
此外,该TextBoxFor方法不会拾取数据注释。您可以直接将格式字符串指定为参数,如下所示:
@Html.TextBoxFor(x => x.Shifts[i].Start, "{0:hh\:mm}")
Or you can switch to EditorForlike this:
或者你可以EditorFor像这样切换:
@Html.EditorFor(x => x.Shifts[i].Start)

