日期不会使用给定的格式值进行验证(MVC 4) [英] Date does not validate with given Format value (MVC 4)
问题描述
查看模型
View Model
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "*")]
public DateTime? TransferDate { get; set; }
Javascript
Javascript
$("#TransferDate").datepicker({ dateFormat: 'mm/dd/yy' });
查看
View
@Html.EditorFor(model => model.TransferDate)
虽然我也在Web中更改了文化信息.Config,参考这篇文章发布
Although I have also change the culture information in Web.Config,Reference to this post post
<globalization uiCulture="en-US" culture="en-US"/>
但仍然验证失败,验证错误值'11 / 21/2014'对TransferDate无效
在控制器中,ModelState.IsValid是false,TransferDate值为空。
请给我一些解决方案。
But still validation is coming false,validation error is The value '11/21/2014' is not valid for TransferDate
In controller, ModelState.IsValid is false,TransferDate value is coming null.
please suggest me some Solution.
推荐答案
( #TransferDate)。datepicker({dateFormat:' mm / dd / yy'});
("#TransferDate").datepicker({ dateFormat: 'mm/dd/yy' });
查看
View
@Html.EditorFor(model => model.TransferDate)
虽然我也在Web.Config中更改了文化信息,但是参考这篇文章 post
Although I have also change the culture information in Web.Config,Reference to this post post
<globalization uiCulture="en-US" culture="en-US"/>
但仍然验证失败,验证错误是值'11 / 21/2014'对TransferDate无效
在控制器中,ModelState.IsValid为false,TransferDate值为空。
please sugg给我一些解决方案。
But still validation is coming false,validation error is The value '11/21/2014' is not valid for TransferDate
In controller, ModelState.IsValid is false,TransferDate value is coming null.
please suggest me some Solution.
你可能需要放置一个ModelBinder。
请参阅: http://weblogs.asp.net/melvynharbour/mvc- modelbinder-and-localization [ ^ ]
http://stackoverflow.com/questions/7889038/asp-net-mvc-datetime-culture-issue-when-passing-value-back-to-controller [ ^ ]
但是对,基于DataAnnotation的验证应该具有文化意识。但看起来你已经完成了所有的事情。不过,我可以想象有一个更简洁的解决方案...
You might need to put a ModelBinder in place.
See: http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization[^]
http://stackoverflow.com/questions/7889038/asp-net-mvc-datetime-culture-issue-when-passing-value-back-to-controller[^]
But you'r right, DataAnnotation based validation should be culture-aware. But it looks you have done everything as you should. Still, I can imagine that there is a neater solution for this...
替换视图
@ Html.EditorFor (model => model.TransferDate)
to
< input type =text name =TransferDateid =TransferDate/>
Replace in View
@Html.EditorFor(model => model.TransferDate)
to
<input type="text" name = "TransferDate" id="TransferDate" />
这篇关于日期不会使用给定的格式值进行验证(MVC 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!