MVC DateTime验证失败

Ral*_*sen 6 validation asp.net-mvc model-validation asp.net-mvc-3 asp.net-mvc-4

我发现了许多simulair问题,但不是一个有效的干净解决方案.我看到很多自定义代码可以让它工作,但为什么会这样?这应该从一开始就不起作用吗?

我认为奇怪的是,在IE9中它可以工作,但在Firefox和Chrome中它失败了.每当我在Firefox或Chrome中尝试时,我都会收到消息"字段生日必须是日期".

当我在新的MVC 4 RTM项目中尝试下面的代码时,我无法让它工作.我在所有浏览器中都看到DateTime.Now默认为dd-MM-yyyy(Holland)但我无法在Firefox和Chrome中提交它.

全局标记未在web.config中设置,因此必须使用默认值.我来自荷兰,所以它应该得到我猜的客户文化.

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    //[DataType(DataType.Date)]
    public DateTime Birthday { get; set; }
}

[AllowAnonymous]
    public ActionResult Register()
    {
        RegisterModel vm = new RegisterModel()
        {
            Birthday = DateTime.Now
        };
        return View(vm);
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                //WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                //WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

标记

<!-- language: lang-none -->
@model DateTimeWithDatePicker.Models.RegisterModel
@{
   ViewBag.Title = "Register";
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>Create a new account.</h2>
</hgroup>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <fieldset>
        <legend>Registration Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Birthday)
                @Html.EditorFor(m => m.Birthday)
            </li>
        </ol>
        <input type="submit" value="Register" />
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Run Code Online (Sandbox Code Playgroud)

gls*_*123 7

我能够通过修改日期的jQuery验证器函数来解决这个问题:

 <script type="text/javascript">

  $(function () {

    var dateFormat="dd/mm/yy"; // en-gb date format, substitute your own

    $("#Birthday").datepicker({
        "dateFormat": dateFormat
    });

    jQuery.validator.addMethod(
        'date',
        function (value, element, params) {
            if (this.optional(element)) {
                return true;
            };
            var result = false;
            try {
                $.datepicker.parseDate(dateFormat, value);
                result = true;
            } catch (err) {
                result = false;
            }
            return result;
        },
        ''
    );

});
Run Code Online (Sandbox Code Playgroud)


Ral*_*sen 3

问题是 jQuery 验证和本地化。似乎有 jQuery 插件的消息和方法的本地化文件。请参阅我的博客,了解该问题的详细说明以及我如何解决它。

http://www.locktar.nl/programming/mvc/localization-validation-in-mvc/

编辑: 我刚刚发布了一篇新的博客文章,刷新了所有本地化问题以及如何修复 DateTime 属性。请参阅我的新帖子MVC 本地化验证