J'utilise Entity Framework et j'essaie d'effectuer une validation sur mes champs Email
et Username
dans la vue utilisateur d'édition. Je sais comment faire la validation mais je n'ai pas trouvé où faire ça.
Voici ma vue :
<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>
Pour les champs Name
et Company
j'ai facilement fait la validation car ils sont tous les deux sur le ApplicationUser
modèle (vérifier ci-dessous). Le problème est que Email
et Username
ne sont pas là parce qu'ils sont hérités.
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; }
Si j'essaie de définir Email
et username
sur le modèle ci-dessus, il est dit qu'il est déjà défini ( Capture d'écran ). Le problème est que je ne peux pas trouver où donc je peux mettre la validation là-bas.
Ils sont définis dans la classe de base comme virtual
:
public virtual string Email { get; set; }
public virtual string UserName { get; set; }
Vous devez donc remplacer ces propriétés et ajouter votre annotation. Comme ça:
[Required(ErrorMessageResourceName = "field_cannot_be_empty",
ErrorMessageResourceType = typeof(FMS.Resources.Global))]
public override string Email { get; set; }
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots