我正在使用实体框架,并试图在编辑用户View上对我的Email
和Username
字段进行验证。我知道如何进行验证,但我找不到在哪里进行验证。
这是我的看法:
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Company, Model.Companies.Select(company => new SelectListItem()
{
Text = company,
Value = company,
Selected = company == Model.Company
}), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10 pull-right">
<button class="btn btn-default" type="reset" onclick="location.href='@Url.Action("Index", "ManageUsers")'"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> @FMS.Resources.Global.back</button>
<button class="btn btn-info" type="submit"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> @FMS.Resources.Global.save</button>
</div>
</div>
</div>
对于Name
和Company
字段,我很容易进行验证,因为它们都在ApplicationUser
模型上(请查看下面的内容(。问题是Email
和Username
不存在,因为它们是继承的。
public class ApplicationUser : IdentityUser
{
[Required(ErrorMessageResourceName = "field_cannot_be_empty",
ErrorMessageResourceType = typeof(FMS.Resources.Global))]
public string Name { get; set; }
public IEnumerable<string> Companies { get; set; }
[Required(ErrorMessageResourceName = "field_cannot_be_empty",
ErrorMessageResourceType = typeof(FMS.Resources.Global))]
public string Company{ get; set; }
如果我试图在上面的模型上定义Email
和username
,它表明它已经定义好了(屏幕截图(。问题是我找不到哪里,所以我可以把验证放在那里。
它们在基类中定义为virtual
:
public virtual string Email { get; set; }
public virtual string UserName { get; set; }
因此,您需要覆盖这些属性并添加注释。像这样:
[Required(ErrorMessageResourceName = "field_cannot_be_empty",
ErrorMessageResourceType = typeof(FMS.Resources.Global))]
public override string Email { get; set; }
As@S.Akbari表示,您可以覆盖基本属性并解决问题。但还有其他方法可以做到这一点。因此,使用DataAnnotations实现的一切都应该使用EFFluent API来实现。
下面是一个例子:
public class YourContext: DbContext
{
public DbSet<IdentityUser> IdentityUser{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUser>().Property(p => p.Email).HasMaxLength(25);
}
}
你应该看看这篇文章-https://msdn.microsoft.com/en-us/library/gg193959(v=vs.113(.aspx