3

我在@using (Ajax.BeginForm()){---} 中有三个 PasswordFor、oldpassword、newpassword 和 confirmnewpassword,我需要这些值与模型绑定,并且需要使用数据注释在客户端验证 newpassword 和 confirmnewpassword 的值. 我使用 [Compare("newpassword",errormessage="error message---"(] 来比较 newpassword 和 confirmnewpassword 的值。当我对 newpassword 和 confirmnewpassword 仅使用两个 PasswordFor 时,[Compare("newpassword"),.. ] 工作得很好但是当旧密码的密码也在 Ajax.BeginForm 中使用时,比较验证不起作用.我需要将所有这三个值通过模型传递给我的控制器,我不能同时使用这三个框将比较新密码和确认新密码的表格。如果 oldpassword 在 BeginForm() 之外使用,则比较有效,但 oldpassword 的值未使用数据注释进行验证,并且其值未通过模型传递给控制器​​。我所需要的只是在我的 Ajax.BeginForm 中拥有所有这三个 PasswordFor 时让这个 [Compare()] 工作,并且在我的所有验证都在客户端验证之前它不会提交。任何帮助将不胜感激。

我的观点:

@model eremit.Models.CustomerAll
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.debug.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>


@using (Ajax.BeginForm("verifycustomer", "CustomerLogin", new AjaxOptions { UpdateTargetId = "messagediv" }))
                {
                    @Html.ValidationSummary(true)


                        <h3>Old password</h3>@Html.PasswordFor(model => model.Login.oldpassword)
                        @Html.ValidationMessageFor(model => model.Login.oldpassword)

                     <h3>New password</h3>@Html.PasswordFor(model => model.Login.newpassword)
                        @Html.ValidationMessageFor(model => model.Login.newpassword)

                        <h3>

                            Re-enter New password</h3>@Html.PasswordFor(model => model.Login.confirmnewpassword)
                        @Html.ValidationMessageFor(model => model.Login.confirmnewpassword)


                        <div id="messagediv">
                        </div>

                            <input type="submit" id="btnchangepassword" value="Change Password" />

我的模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace eremit.Models
{
    public class Login
    {
    [Required(ErrorMessage = "* Please enter old password")]
    public string oldpassword { get; set; }

    [Required(ErrorMessage = "* Please enter new password")]
    [Display(Name = "newpassword")]
    [RegularExpression("^[a-zA-Z<>$@#!%&_0-9]+$", ErrorMessage = "Invalid Password")]
    public string newpassword { get; set; }

    [Required(ErrorMessage = "* Re-enter your new password")]
    [Compare("newpassword", ErrorMessage = "The new password and confirmation password     do not match.")]
    public string confirmnewpassword { get; set; }
}
}
4

1 回答 1

2

Since MVC 3 is introduced,the Microsoft libraries such as MicrosoftAjax.js, MicrosoftMvcAjax.js and MicrosoftMvcValidation.js are obsolete.

These libraries are replaced by jquery.validate.min.js, jquery.unobtrusive-ajax.min.js and jquery.validate.unobtrusive.min.js as part of MVC3.Just comment the Microsoft helpers and try.

MicrosoftAjax.js
MicrosoftAjax.debug.js
MicrosoftMvcAjax.js
MicrosoftMvcAjax.debug.js
于 2013-10-12T10:36:49.007 回答