2017-06-25 3 views
0

Neu in ASP.NET Core.Display Friendly Fehlermeldung bei der Verwendung von Dekoratoren in Modellen

Ich habe einen Blick:

<form asp-action="LogIn"> 
    <div class="form-horizontal"> 
     <hr /> 
     <div asp-validation-summary="ModelOnly" class="text-danger"></div> 
     <div class="form-group"> 
      <label asp-for="EmailAddress" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="EmailAddress" class="form-control" style="min-width:350px" /> 
       <span asp-validation-for="EmailAddress" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Password" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Password" class="form-control" style="min-width:350px" /> 
       <span asp-validation-for="Password" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Enter" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</form> 

Ich habe dieses Modell für sie:

public class Subscription 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public long SubscriptionId { get; set; } 

    [Display(Name = "Email Address")] 
    [Required] 
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]   
    public string EmailAddress { get; set; } 

    [NotMapped] 
    public string Password { get; set; }   
} 

Also, wenn ein Benutzer in einer E-Mail-Adresse, die nicht durch den regulären Ausdruck validiert ich die Fehlermeldung:

enter image description here

Wie io ver-fahren diese 'default' Fehlermeldung zu sagen (zum Beispiel):

E-Mail-Adresse ist ungültig

?

Antwort

1

Sie müssen wie this-

[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Your friendly message goes here")] 
+0

Dank ErrorMessage Eigenschaft zum RegularExpression Attribut hinzufügen. Fühle dich jetzt dumm. Brauche mehr Kaffee :) –

Verwandte Themen