与文化格式日期在剃刀@ Html.EditFor [英] Format Date with culture in Razor @Html.EditFor

查看:139
本文介绍了与文化格式日期在剃刀@ Html.EditFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 4,C#。

在我的Razor视图我有:

In my razor view I have:

@Html.EditorFor(model => model.ActionObject.StartDate)

这是注释在我的模型:

Which is annotated in my model with:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public System.DateTime? StartDate { get; set; }

在视图中,它被设置像这样,随着日期选择器:

In the view, it is set like so, with date picker:

注意它是如何MM / DD / YYYY。如何动态更改此基础上,用户?用户可以在那里的文化设置任何他们想要的。

Notice how it is mm/dd/yyyy. How can I change this dynamically based on the user? The user can set there culture to whatever they want.

通常我可以这样做:

.ToString("d", culture)

当文化动态设置。但是,这并不为@ Html.Editfor工作。

Where culture is set dynamically. But this doesn't work for @Html.Editfor.

推荐答案

日期选择器是HTML 5的输入类型日期谷歌浏览器的原生再presentation 。你不能改变这种格式。显示格式来自用户的文化/本地化配置在他的操作系统。

This datepicker is the native representation for HTML 5 input type date in Google Chrome. You can't change this format. The display format comes from user Culture / Localization config in his OS.

如果您想格式化成你想要的,删除 [数据类型(DataType.Date)] ,渲染默认的输入字段并然后用 JQuery用户界面DatePicker的或另一个呈现一个更好的日期字段。

If you want to format this as you want, remove the [DataType(DataType.Date)], rendering a default input field and then use a JQuery UI DatePicker or another one to render a better date field.

这篇关于与文化格式日期在剃刀@ Html.EditFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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