在我的MVC4项目中的某个模型中,我有DateTime
属性。在客户端,我使用jquery datepicker
。我希望格式为'dd.mm.yyyy‘。我为属性设置属性
[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
});
我还验证了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" />
你有什么想法吗?
发布于 2013-04-03 08:41:30
在将值提交给操作时,是否使用GET或POST谓词?如果您使用的是GET,则datetime值应始终采用固定的区域性格式。
如果您使用的是POST,那么web.config全球化设置应该足以接受所需格式的日期字符串。
https://stackoverflow.com/questions/15780456
复制相似问题