用户名和电子邮件验证

时间:2017-08-30 10:45:54

标签: c# asp.net asp.net-mvc asp.net-identity

我正在使用Entity Framework并尝试对编辑用户View上的EmailUsername字段进行验证。我知道如何进行验证,但我找不到在哪里做。

这是我的观点:

<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>

对于NameCompany字段,我很容易进行验证,因为它们都在ApplicationUser模型上(请参见下文)。问题是EmailUsername不存在,因为它们是继承的。

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; }

如果我尝试在上面的模型上定义Emailusername,则说它已经定义(Screenshot)。问题是我找不到哪里,所以我可以在那里进行验证。

2 个答案:

答案 0 :(得分:1)

它们在基类中定义为virtual

public virtual string Email { get; set; }
public virtual string UserName { get; set; }

所以你需要override这个属性并添加你的注释。像这样:

[Required(ErrorMessageResourceName = "field_cannot_be_empty", 
          ErrorMessageResourceType = typeof(FMS.Resources.Global))]
public override string Email { get; set; }

答案 1 :(得分:0)

正如@ S.Akbari所说,你可以覆盖基本属性并解决问题。但是还有其他方法可以做到这一点。因此,使用EF Fluent API 可以实现 DataAnnotations 所实现的一切。

所以这是一个例子:

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