MVC与 日期格式

发布于 2024-07-13 16:48:21 字数 230 浏览 10 评论 0原文

在基于 MVC 的架构中,日期格式(长/短/时间/无时间/英国/美国)应位于何处? 我希望能够在每个字段的基础上指定长/短/时间/无时间 - 即 DateOfBirth 不关心时间,但 CreationDate 可能。

使用 ASP.NET MVC,似乎没有一种简单的方法可以在视图中执行此操作,这让我想知道它是否应该作为 FormattedDate 字段放在 ViewModel 中?

有什么建议么?

Where in an MVC based architecture should the date formatting (long/short/time/no-time/UK/US) live? I want to be able to specify long/short/time/no-time on a per-field basis - i.e. DateOfBirth doesn't care about time, but CreationDate might.

Using ASP.NET MVC, there doesn't seem an easy way to do it in the View, which makes me wonder if it should sit in the ViewModel as a FormattedDate field?

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧桑㈠ 2024-07-20 16:48:21

首先,(1:长/短)、(2:有时间/无时间)和(3:英国/美国)之间存在区别。 1. 绝对是一个视图选择,2. 有效地描述数据类型(“DateTime”、“Date”(不存在,您需要创建)),3. 是用户配置文件,这又是属于视图中。

到目前为止,您只有两种类型 DateTime (存在)和 Date (不存在)。 这些是您可以期望模型公开的类型。

First of all there is a difference between (1:long/short), (2:time/no-time) and (3:UK/US). 1. is definitely a view choice to make, 2. Effectively describes to data types ("DateTime", "Date" (which doesn't exist and you'd need to create)) and 3. is user profile thing, which again belongs in the view.

So far then you only have two types DateTime (which exists) and Date (which doesn't). These are the types that you can expect the model to expose.

吾性傲以野 2024-07-20 16:48:21

我将其分为两部分:

  • 格式化(长、短)
  • 数据类型(日期和时间、仅日期、仅时间)

第一部分主要是视图问题,应该存在于演示文稿中。 我很可能只是为 DateTime/Date/Time 制作一些扩展方法,其中包括所需的翻译,甚至可能创建类型的演示版本。

第二部分是模型问题,因为出生日期(通常)不包括时间,无论您如何呈现它,并且重复讲座的开始时间不应该有日期(因为它是重复的,所以它会实际上有一个开始/结束)。 遗憾的是,.net 不包含(据我所知)单独的日期和时间对象,所以我通常会自己推出。

I read this as two parts:

  • Formatting (long, short)
  • Data type (date and time, just date, just time)

The first part is a view concern, mostly, and should live in the presentation. I'd most likely just craft some extension methods for the DateTime/Date/Time that include the required translation or perhaps even create presentation versions of the types.

The second part are model concerns, since a date of birth (usually) doesn't include time, regardless of the way you present it, and a starting time for a recurring lecture shouldn't have a date (since it's recurring, it would actually have a start/end). Sadly .net doesn't include (as far as I can recall) separate objects for Date and Time, so I roll my own, usually.

一向肩并 2024-07-20 16:48:21

我在这些情况下使用 Htmlhelpers。
这是一种原始的方式,用于返回丹麦首选格式的金额(猜猜我住在哪里)。
我有类似的返回日期和时间。

public static class FormatMoneyExtension {
    public static string FormatMoney(this HtmlHelper htmlHelper, decimal? amount) {
        if (null != amount) {
            return ((decimal)amount).ToString("N", System.Globalization.CultureInfo.CreateSpecificCulture("da-DK"));
        } else {
            return new decimal(0.0).ToString("N", System.Globalization.CultureInfo.CreateSpecificCulture("da-DK"));
        }
    }
}

它使视图更具可读性,即:<%:Html.FormatMoney(Model.amount)%>

您可以以类似的方式包装本机控件,例如 texbox 等。

祝你好运,工作愉快

I use Htmlhelpers in these situations.
This is a primitive one for returning amounts of money formated as preferred in Denmark( guess where I live).
I have similar for returning dates and time.

public static class FormatMoneyExtension {
    public static string FormatMoney(this HtmlHelper htmlHelper, decimal? amount) {
        if (null != amount) {
            return ((decimal)amount).ToString("N", System.Globalization.CultureInfo.CreateSpecificCulture("da-DK"));
        } else {
            return new decimal(0.0).ToString("N", System.Globalization.CultureInfo.CreateSpecificCulture("da-DK"));
        }
    }
}

It makes the views much more readable ie:<%:Html.FormatMoney(Model.amount)%>.

You can wrap the native controls, like texbox etc. in a similar way.

Good luck and happy working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文