ASP.NET MVC2 Model Validation Fails with Non-US Date Format

Posted by 81bronco on Stack Overflow See other posts from Stack Overflow or by 81bronco
Published on 2010-05-17T18:38:54Z Indexed on 2010/05/17 18:40 UTC
Read the original article Hit count: 778

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.DateOfBirth) %>
                </div>
                <div class="editor-field">
                    <%= Html.EditorFor(model => model.DateOfBirth) %>
                    <%= Html.ValidationMessageFor(model => model.DateOfBirth) %>
                </div>

                <p><input type="submit" value="Save" /></p>
            </fieldset>

        <% } %>
    </body>
</html>

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about dataannotations