格式化MVC模式的时间跨度场 [英] Formatting MVC model TimeSpan field

查看:226
本文介绍了格式化MVC模式的时间跨度场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个timepickers一个MVC页面。这些被存储在可为空时间跨度模型中的对象的列表。问题是,我得到印有秒到输入字段中的时间跨度。是否有某种方式让我得到了一些其他的方式印刷的时间跨度(如 7:00 来格式化这些模特属性,而不是 07:00 :00 )?不用说,我的的建议[DisplayFormat ...] 没有工作。至少不是我的方式希望。

的输入字段被这样定义:

  @ Html.TextBoxFor(X => x.Shifts [I]。开始)

该模型的相关部分如下:

 公共类轮班
{
    [DisplayFormat(这里的东西可能?)
    公开时间跨度?启动{搞定;组; }
    公开时间跨度?结束{搞定;组; }
    公开时间跨度?暂停{搞定;组; }
}公共类TimeRegistrationViewModel
{
    公开名单<&轮班GT;班次{搞定;组; }    ...
}

鸭preciate它一如既往!


解决方案

  [DisplayFormat(DataFormatString ={0:\\\\ HH:MM},ApplyFormatInEditMode = TRUE)]
公开时间跨度?启动{搞定;组; }[DisplayFormat(DataFormatString ={0:\\\\ HH:MM},ApplyFormatInEditMode = TRUE)]
公开时间跨度?结束{搞定;组; }[DisplayFormat(DataFormatString ={0:\\\\ HH:MM},ApplyFormatInEditMode = TRUE)]
公开时间跨度?暂停{搞定;组; }

但千万记住,为时间跨度的主要目的是重新present时间的测量时间,没有一天时间。这意味着时间跨度值可能超过24小时。它们也可以是负的,要重新present时间轴上向后移动。

这是接受的使用它们随着时间的日,并且实际上是由框架本身(完成例如, DateTime.TimeOfDay )。但是,使用这种方法的时候,你应该仔细验证用户输入。如果你仅仅依靠数据类型,用户也许能够输入有效的时间跨度,但不是有效的一天时间值。例如, -1.22:33:44 是一个有效的时间跨度的再presents1天22小时33分44秒的过去

这将是容易得多,如果净了原生时间类型,但它没有。

此外, TextBoxFor 方法不会拿起数据注解。您可以直接指定格式字符串作为参数,如:

  @ Html.TextBoxFor(X => x.Shifts [I]。开始,{0:\\\\ HH:MM})

或者你也可以切换到 EditorFor 是这样的:

  @ Html.EditorFor(X => x.Shifts [I]。开始)

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.

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!

解决方案

[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 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.

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.

It would be so much easier if .Net had a native Time type, but it does not.

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}")

Or you can switch to EditorFor like this:

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

这篇关于格式化MVC模式的时间跨度场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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