我正在使用MVC数据库第一种方法。我的数据库没有确认密码字段。所以我在我创建的部分类中添加了这个字段,并使用了[NotMapped]
属性。
数据库生成的课程
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; }
}
}
我创建的部分课程
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; }
}
}
查看比较密码
<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>
但由于某种原因,它不起作用且Model.state无效,它会抛出错误&#34;密码和确认密码不匹配。&#34;