Compare attribute always validate on the first element of my view
Please help on the problem stated above.
Heres my view
<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>
Here is my model
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"开发者_Go百科, 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; }
}
And my viewmodel..
public class PersonInformation
{
public Person person { get; set; }
public Address premiseAddress { get; set; }
public Address billingAddress { get; set; }
}
In this case.. my confirm password will validate on my username element in the view..
Any help is highly appreciated.
Thanks
Since my first element in the view is the username field.. my confirm password field will validate on that field and not on the password field.
My code will produce something like this.
USERNAME : ADMIN
PASSWORD : PASSWORD
CONFIRM : PASSWORD **<< this will states that "The password and confirmation password do not match."
BUT if I input something like this
USERNAME : ADMIN
PASSWORD : PASSWORD
CONFIRM : ADMIN
The validation error is gone., but it should not supposed to behave like that.
Heres the generated 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>
The controller
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);
}
精彩评论