如何解决错误消息,值'27 / 06/2014'对于MVC 4中的日期无效 [英] How do I resolve error message, The value '27/06/2014' is not valid for Date in MVC 4

查看:51
本文介绍了如何解决错误消息,值'27 / 06/2014'对于MVC 4中的日期无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以这种格式选择日期时收到模型生成错误消息27/06/2014(任何日期超过12 / mm / yy)。



错误消息是:值'27 / 06/2014'对于MVC 4中的日期无效



之前我的模型和视图是这样的:



查看代码部分:





@ Html.TextBoxFor(model => model.DateOfBirth,new {style =width:310px; height:25px})

@ Html.ValidationMessageFor(model => model.DateOfBirth)< br $>





型号代码部分:



[必填( ErrorMessage =需要出生日期字段)]

[DisplayName(出生日期:)]

// [DataType(DataType.Date)]

public System.DateTime? DateOfBirth {get;组; }



注意:这里即使我为DateOfBirth模型注释了DataType部分,我也收到了错误消息。因为它不是来自模型实际上它来自控制器端。



请帮助。



在此先感谢

解决方案

解决方案:

在Web Config中添加:

< system.web >
< 全球化 culture = en-US / >
< / system.web >

在Global.asax.cs中添加:

protected void Application_BeginReque st()
{
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern =dd.MM.yyyy;
System.Threading.Thread.CurrentThread.CurrentCulture = info;
}

参考此链接:

http://coderskey.blogspot.in/2014/06/the-value-27062014-is-not-valid -for.html


I am getting an model generated error message while choosing date in this format 27/06/2014 (any date more then 12/mm/yy).

Error Message is: The value '27/06/2014' is not valid for Date in MVC 4

Previously my Model and View was like this:

View Code Portion:


@Html.TextBoxFor(model => model.DateOfBirth, new { style = "width:310px;height:25px" })
@Html.ValidationMessageFor(model => model.DateOfBirth)



Model Code Portion:

[Required(ErrorMessage = "Date of birth field is required")]
[DisplayName("Date of Birth:")]
//[DataType(DataType.Date)]
public System.DateTime? DateOfBirth { get; set; }

Note: Here even if I had DataType portion commented for DateOfBirth model, I was getting error message. As it was not coming from model actually it was coming from controller end.

Please Help.

Thanks in advance

解决方案

Solution For This:

In Web Config add this:

<system.web>
    <globalization culture="en-US" />
</system.web>

In Global.asax.cs add this:

 protected void Application_BeginRequest()
        {
            CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
            info.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
            System.Threading.Thread.CurrentThread.CurrentCulture = info;
        }

Refer this link: 

http://coderskey.blogspot.in/2014/06/the-value-27062014-is-not-valid-for.html


这篇关于如何解决错误消息,值'27 / 06/2014'对于MVC 4中的日期无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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