我在我的模型中使用DataType.Date属性,在我的视图中使用EditorFor.这在Internet Explorer 8和Internet Explorer 9中运行良好,但在谷歌浏览器中它显示日期选择器,而不是显示值,它只是在褪色的灰色文本中显示"月/日/年".
为什么Google Chrome不会显示价值?
模型:
[DataType(DataType.Date)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
视图:
<td class="fieldLabel">Est. Pur. Date</td>
<td class="field">@Html.EditorFor(m=>m.EstPurchaseDate)</td>
Run Code Online (Sandbox Code Playgroud)


是否可以借助方法显示dd/mm/yyyy格式的DateTime值?我尝试通过使用一些格式并在相关属性中添加一些注释来完成此操作,如下所示,但它没有任何意义.任何帮助解决这个问题将不胜感激.HTML HeplerAsp.NET MVC@Html.LabelFor
模型:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> RegistrationDate { get; set; }
Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC 3.
我的ViewModel看起来像这样:
public class Foo
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
在视图中,我有这样的事情:
<div class="editor-field">
@Html.EditorFor(model => model.StartDate)
<br />
@Html.ValidationMessageFor(model => model.StartDate)
</div>
Run Code Online (Sandbox Code Playgroud)
StartDate以正确的格式显示,但当我将其值更改为19.11.2011并提交表单时,我收到以下错误消息:"值'19 .11.2011'对StartDate无效."
任何帮助将不胜感激!
我将MVC3解决方案升级为MVC4.迁移后,验证程序已损坏.
如果我选择德语作为语言,我的输入日期是"20.03.2013".我在MVC4中得到验证错误,但在MVC3中没有.如果我将格式从"20.03.2013"替换为"20/03/2013",它适用于MVC4,但不适用于MVC3 ;-)
我将当前线程的UI文化设置为德语.ResX值的输出使用正确的语言,因此我知道文化不应该出错.,仅适用于网站本身.错误消息是英文的,但该网站是德语.
我认为这意味着验证器使用了错误的UI文化.
这是我使用的代码.
[Required(ErrorMessageResourceName = "Error_DepartureDate", ErrorMessageResourceType = typeof(Resx.Query))]
public DateTime? DepartureDate { get; set; }
我假设默认模型绑定器有问题,因为渲染的html看起来很好:
data-lang="de" data-mindate="3" data-val="true" data-val-required="Bitte geben Sie das gewünschte Reisedatum des Hinflugs ein." id="DepartureDate" name="DepartureDate" tabindex="3" type="text" value=""
我将Jscript升级到使用Visual Studio 2012(已安装SP1)模板创建新Mvc应用程序时发布的源代码.这没有任何影响.
我有一个CultureModelBinder,它从Session中读取当前的文化,并使用一个小帮助函数设置文化.
public static void UpdateThreadCulture(CultureInfo culture)
{
Thread.CurrentThread.CurrentUICulture = culture;
}
culture文件模型binder是默认的binder.
ModelBinders.Binders.DefaultBinder = new CultureModelBinder(); ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder()); // and many, many more
也许mvc4的执行顺序发生了变化,导致问题?
更新:该项目使用.NET Framework 4.5作为目标.
更新2:
我有一个组合框,用户可以选择16种不同的语言,每种语言都有自己特定的格式.
例如DE-de - > DD.MM.YYYY; en-en - > DD/MM/YYYY; …