ASP.NET MVC2模型验证非美国日期格式失败 [英] ASP.NET MVC2 Model Validation Fails with Non-US Date Format
问题描述
我有一个显示在两种文化的小MVC2应用:EN-US和ES-MX。一个部分包含对是$ P $对填充在示范的当前日期的日期的用户输入。
在使用EN-US,日期字段显示为MM / DD / YYYY,可以使用相同的格式被改变,而不会造成任何验证错误。
在使用ES-MX,日期字段显示为DD / MM / YYYY,但当日期这种格式进行编辑时,服务器端验证失败的消息:
值'17 / 05/1991年'是无效的日期。
块引用>其中之一,在跳出我有关消息的第一件事情是,它没有本地化。无论是消息本身(我不认为我能控制)和域的显示名称(这是我能够控制在我的code为本地化)。应显示在本地化的格式。
我曾尝试通过code步进,看看到底哪里验证失败,但似乎里面的一些编译MVC或DataAnnotations code,我不能看到的要发生的事情。
应用程序的详细信息:IIS6,ASP.NET 3.5(C#),MVC 2 RTM
样本模型code:
公共类TestVieModel {
[LocalizedDisplayNameDisplayName(TheDateDisplayName,NameResourceType = typeof运算(Resources.Model.TestViewModel))]
[必填(ErrorMessageResourceName =TheDateValidationMessageRequired,ErrorMessageResourceType = typeof运算(Resources.Model.TestViewModel))]
[数据类型(DataType.Date)
公众的DateTime TheDate {搞定;组; }
}样控制器动作code:
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult保存(TestViewModel模型){
如果(ModelState.IsValid){//< ---使用ES-MX和foramtted为DD / MM / YYYY的日期时,始终是假的。
//做其他的东西
返回this.View(完成,模型);
} //验证失败,重新显示形式。
返回this.View(回车,模型);
}样品查看code:
<%@页面语言=C#继承=System.Web.Mvc.ViewPage< HispanicSweeps.Web.Model.LosMets.EnterViewModel>中%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>测试与LT; /标题>
< /头>
<身体GT;
&下;使用%(Html.BeginForm()){%GT;
<%= Html.ValidationSummary(真)%GT; <&字段集GT;
<传奇>&领域LT; /传说>
< DIV CLASS =编辑标记>
<%= Html.LabelFor(型号=> model.TheDate)%GT;
< / DIV>
< DIV CLASS =主编场>
<%= Html.EditorFor(型号=> model.TheDate)%GT;
<%= Html.ValidationMessageFor(型号=> model.TheDate)%GT;
< / DIV> < P><输入类型=提交值=保存/>< / P>
< /字段集> <%}%GT;
< /身体GT;
< / HTML>
解决方案下面就是我解决了这个问题,我的情况。我手动验证控制器中的日期和重置的ModelState该属性
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult保存(TestViewModel模型){ VAR tempDate =新的DateTime();
VAR文化= CultureInfo.CurrentUICulture; 如果(DateTime.TryParse(的Request.Form [TheDate],文化,DateTimeStyles.None,出tempDate)){
model.DateOfBirth = tempDate;
ModelState.Remove(TheDate);
} 如果(ModelState.IsValid){//< ---现在有效
//做其他的东西
返回this.View(完成,模型);
} //验证失败,重新显示形式。
返回this.View(回车,模型);
}I have a small MVC2 app that displays in two cultures: en-US and es-MX. One portion contains a user input for a date that is pre-populated with the current date in the Model.
When using en-US, the date field is displayed as MM/dd/yyyy and can be changed using the same format without causing any validation errors.
When using es-MX, the date field is displayed as dd/MM/yyyy, but when the date is edited in this format, the server-side validation fails with the message:
The value '17/05/1991' is not valid for The Date.
One of the first things that jumps out at me about that message is that it is not localized. Both the message itself (which I do not think I can control) and the Display Name of the field (which I can control and is localized in my code). Should be displaying in a localized format.
I have tried stepping through the code to see exactly where the validation is failing, but it seems to be happening inside some of the compiled MVC or DataAnnotations code that I cannot see.
Application details: IIS6, ASP.NET 3.5 (C#), MVC 2 RTM
Sample Model Code:
public class TestVieModel{ [LocalizedDisplayNameDisplayName("TheDateDisplayName", NameResourceType=typeof(Resources.Model.TestViewModel))] [Required(ErrorMessageResourceName="TheDateValidationMessageRequired", ErrorMessageResourceType=typeof(Resources.Model.TestViewModel))] [DataType(DataType.Date)] public DateTime TheDate { get; set; } }
Sample Controller Action Code:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Save(TestViewModel model) { if(ModelState.IsValid) { // <--- Always is false when using es-MX and a date foramtted as dd/MM/yyyy. // Do other stuff return this.View("Complete", model); } // Validation failed, redisplay the form. return this.View("Enter", model); }
Sample View Code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HispanicSweeps.Web.Model.LosMets.EnterViewModel>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Test</title> </head> <body> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <div class="editor-label"> <%= Html.LabelFor(model => model.TheDate) %> </div> <div class="editor-field"> <%= Html.EditorFor(model => model.TheDate) %> <%= Html.ValidationMessageFor(model => model.TheDate) %> </div> <p><input type="submit" value="Save" /></p> </fieldset> <% } %> </body> </html>
解决方案Here's how I solved the issue in my case. I manually validated the date in the controller and reset the ModelState for that property:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Save(TestViewModel model) { var tempDate = new DateTime(); var culture = CultureInfo.CurrentUICulture; if(DateTime.TryParse(Request.Form["TheDate"], culture, DateTimeStyles.None, out tempDate)) { model.DateOfBirth = tempDate; ModelState.Remove("TheDate"); } if(ModelState.IsValid) { // <--- Now valid // Do other stuff return this.View("Complete", model); } // Validation failed, redisplay the form. return this.View("Enter", model); }
这篇关于ASP.NET MVC2模型验证非美国日期格式失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!