I am using MVC database first approach. My database is not having Confirm password field. So I have added this field in the partial class which I created and used [NotMapped]
attribute.
Database generated class
namespace OnlineTest
{
public partial class User
{
public User()
{
this.tbl_purchase = new HashSet<Purchase>();
}
public int UserId { get; set; }
public string username { get; set; }
public string password { get; set; }
public string email { get; set; }
public string PhoneNo { get; set; }
public virtual ICollection<Purchase> tbl_purchase { get; set; }
}
}
Partial class which I created
namespace OnlineTest
{
[MetadataType(typeof(UserMetadata))]
public partial class User
{
[DataType("Password")]
[NotMapped]
[System.Web.Mvc.Compare("password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class UserMetadata
{
[Required(ErrorMessage = "Please type a username")]
[Display(Name = "UserName")]
public string username { get; set; }
[Required(ErrorMessage = "Please type a EmailId")]
[EmailAddress(ErrorMessage = "E-mail is not valid")]
[Display(Name = "Email address")]
public string email { get; set; }
[Required(ErrorMessage = "Please type a Phone number")]
[Display(Name = "Phone Number")]
[StringLength(10, MinimumLength = 10, ErrorMessage = "Your mobile not corrct")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "You have to enter only numbers 111-111-1111")]
public string PhoneNo { get; set; }
[Required(ErrorMessage = "Please type a password")]
[Display(Name = "Password")]
public string password { get; set; }
}
}
View for Compare password
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new
{ @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
But for some reason, it is not working and Model.state is not valid it is throwing error "The password and confirmation password do not match."