ASP.NET MVC2 模型验证因非美国日期格式而失败

发布于 2024-09-02 01:48:28 字数 2470 浏览 4 评论 0原文

我有一个小型 MVC2 应用程序,可以以两种文化显示:en-US 和 es-MX。一部分包含用户输入的日期,该日期预先填充了模型中的当前日期。

使用 en-US 时,日期字段显示为 MM/dd/yyyy,并且可以使用相同的格式进行更改,而不会导致任何验证错误。

使用 es-MX 时,日期字段显示为 dd/MM/yyyy,但是当以此格式编辑日期时,服务器端验证失败并显示消息:

值“17/05/1991”对于日期无效。

关于该消息,我首先想到的一件事是它不是本地化的。消息本身(我认为我无法控制)和字段的显示名称(我可以控制并在我的代码中本地化)。应以本地化格式显示。

我尝试单步执行代码以准确查看验证失败的位置,但它似乎发生在一些我看不到的已编译的 MVC 或 DataAnnotations 代码中。

应用程序详细信息:IIS6、ASP.NET 3.5 (C#)、MVC 2 RTM

示例模型代码:

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; }
}

示例控制器操作代码:

[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);
}

示例视图代码:

<%@ 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-09-09 01:48:28

以下是我如何解决我的案例中的问题。我手动验证了控制器中的日期并重置了该属性的 ModelState:

[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);
    }

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);
    }
感悟人生的甜 2024-09-09 01:48:28

验证发生在 DataAnnotations 类中。您可以根据自己的目的对 DataAnnotations 类进行子类化。

我将创建一个新的 MultiCultureDateType DataAnnotations 类,该类将验证跨多种文化的日期。

更多信息:http://haacked.com/archive/2009 /11/19/aspnetmvc2-custom-validation.aspx

The validation occurs within the DataAnnotations class. You can subclass DataAnnotations classes for your own purposes.

I would create a new MultiCultureDateType DataAnnotations class that will validate dates across multiple cultures.

More information: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文