2013-04-03 103 views
0

在我的mvc 4项目中,某些模型的DateTime属性。在客户端,我使用jquery datepicker。我想要格式为'dd.mm.yyyy'。我的属性设置为属性不绑定模型的DateTime属性

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")] 
    [Required(AllowEmptyStrings = false, ErrorMessage = "* Field is required")] 
    public DateTime DateBuy { get; set; } 

在* js文件我设定的日期格式:

$(".datepicker").datepicker({ 
    changeMonth: true, 
    changeYear: true, 
    showOtherMonths: true, 
    selectOtherMonths: true, 

    dateFormat: "dd.mm.yy", 
    buttonImageOnly: true 
}); 

,也是我ovverided jQuery的验证:

jQuery.validator.addMethod(
    'date', 
    function (value, element, params) { 
     return value.match(/^\d\d?\.\d\d?\.\d\d\d\d$/); 
    }, 
    '' 
); 

但是,当模型来行动模型无效。错误:"The value '24.02.2013' is not valid for DateBuy."

如果日期将为02.24.2013所有工作正常(服务器预期日期的格式为'mm.dd.yyyy')。我试过web.config的变化文化:

<globalization culture="ru-RU" uiCulture="ru-RU" enableClientBasedCulture="true" /> 

你有什么想法吗?

回答

0

您是否使用GET或POST动词将值提交给您的操作? 如果您使用GET,那么日期时间值应始终保持不变的文化格式。

如果您使用POST比web.config全球化设置应该足以接受所需格式的日期字符串。

+0

我使用POST。我在web.config中添加了全球化标记,但它不能解决我的问题 – user571874

+0

您可以分享视图和控制器代码吗?您正在使用<全球化文化=“RU-RU”uiCulture =“RU-RU”enableClientBasedCulture =“true”/> enableClientBasedCulture为true,使其为false或将浏览器配置为使用俄罗斯文化 –