Compare属性总是在我视图的第一个元素上验证

时间:2011-10-10 11:17:50

标签: asp.net-mvc-3 validation

请帮助解决上述问题。

继承我的观点

    <div class="editor-label">
        @Html.LabelFor(model => model.person.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.person.UserName) 
        @Html.ValidationMessageFor(model => model.person.UserName) 
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.person.Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.person.Password) 
        @Html.ValidationMessageFor(model => model.person.Password) 
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.person.ConfirmPassword)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.person.ConfirmPassword) 
        @Html.ValidationMessageFor(model => model.person.ConfirmPassword) 
    </div>

这是我的模特

public abstract partial class Member
{
    [DisplayName("ID")]
    public string ID { get; set; }

    [Required]
    [DisplayName("Username")]
    public string UserName { get; set; }

    [DisplayName("Date Applied")]
    public System.DateTime? DateApplied { get; set; }

    [DisplayName("Date Membered")]
    public System.DateTime? DateMembered { get; set; }

    [DisplayName("Member Type")]
    public int MemberTypeFlag { get; set; }

    [Required]
    [DisplayName("Email Address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}


public class Person : Member
{
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Date Of Birth")]
    public System.DateTime DateOfBirth { get; set; }
}

我的观点模型..

public class PersonInformation
{
    public Person person { get; set; }
    public Address premiseAddress { get; set; }
    public Address billingAddress { get; set; }
 }

在这种情况下..我的确认密码将在视图中的用户名元素上验证..

非常感谢任何帮助。

由于

由于我在视图中的第一个元素是用户名字段..我的确认密码字段将在该字段上验证,而不是在密码字段上验证。

我的代码会产生类似的东西。

 USERNAME : ADMIN
 PASSWORD : PASSWORD
 CONFIRM  : PASSWORD **<< this will states that "The password and confirmation password do not match."

但是如果我输入这样的东西

 USERNAME : ADMIN
 PASSWORD : PASSWORD
 CONFIRM  : ADMIN

验证错误已经消失。但它不应该表现得那样。

继承生成的HTMLcode ..

                                    
    <div class="editor-label">

        <label for="person_Password">Password</label>

    </div>

    <div class="editor-field">

        <input class="text-box single-line password" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="person_Password" name="person.Password" type="password" value="" /> 

        <span class="field-validation-valid" data-valmsg-for="person.Password" data-valmsg-replace="true"></span> 

    </div>



    <div class="editor-label">

        <label for="person_ConfirmPassword">Confirm password</label>

    </div>

    <div class="editor-field">

        <input class="text-box single-line password" data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="person_ConfirmPassword" name="person.ConfirmPassword" type="password" value="" /> 

        <span class="field-validation-valid" data-valmsg-for="person.ConfirmPassword" data-valmsg-replace="true"></span> 

    </div>

控制器

    public ActionResult PersonInformation()
    {
        var pi = new PersonInformation();
        pi.MemberContacts = pi.LoadMemberContacts();
        return View(pi);
    }

    [HttpPost]
    public ActionResult PersonInformation(PersonInformation member)
    {
        if (ModelState.IsValid)
        {
            MembershipCreateStatus createStatus;
            db.Apply(member, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                ModelState.Clear();
                IEnumerable<MemberRequirement> mr = member.person.MemberRequirements;

                return View("Requirements", mr);
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }

        }
        return View(member);
    }

0 个答案:

没有答案