2017-12-23 19 views
-1

私はasp.net(ビジュアルスタジオ2017)でモデルを構築しましたが、コードにいくつかのバリデーションを入れたいと思います。私はこのmessasgeを持っているデータベースを更新しようとしているときに正規表現 - asp.net、c# - 検証に失敗しました

は:

検証は、一の以上のエンティティに失敗しました。詳細については、「EntityValidationErrors」プロパティを参照してください。

正規表現を削除するときに問題はありません。

これは私のコードです:

public class Customer 
{ 
    public int customerID { set; get; } 

    [Display(Name = "First Name")] 
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Must be alphabates only")] 
    [StringLength(20, MinimumLength = 2)] 
    public string firstName { set; get; } 

    [Display(Name = "Last Name")] 
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Must be alphabates only")] 
    [StringLength(20, MinimumLength = 2)] 
    public string lastName { set; get; } 

    [Display(Name = "Phone")] 
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Must be numbers only")] 
    [StringLength(10, MinimumLength = 9)] 
    public string phone { set; get; } 

    [Display(Name = "Car Number")] 
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Must be numbers only")] 

    public int carNumber { set; get; } 


    [Display(Name = "e-mail Customer")] 
    [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be valid email")] 
    [StringLength(30, MinimumLength = 4)] 
    public string mailCustomer { set; get; } 


    [Display(Name = "Home Address")] 
    [RegularExpression(@"^[a-zA-Z0-9''-'\s]*$", ErrorMessage = "Must be alphabates and numbers only")] 
    [StringLength(40, MinimumLength = 2)] 
    public string homeAddress { set; get; } 


    [Display(Name = "Work Address")] 
    [RegularExpression(@"^[a-zA-Z0-9''-'\s]*$", ErrorMessage = "Must be alphabates and numbers only")] 
    [StringLength(40, MinimumLength = 2)] 
    public string workAddress { set; get; } 
} 

これは正しい構文ですか?私はそれらを何度もチェックしています。

これは私がDBを入力する項目の一つである:

new Customer {customerID=311111111, firstName= "Lior", lastName="David", phone="0549121111", carNumber=57382561, mailCustomer="[email protected]", homeAddress="bograshov 22", workAddress="bograshov 18"}, 
+0

だから、あなたはEntityValidationErrorsプロパティをチェックするとき、何それはあなたを教えてくれありませんか? –

+0

どうすれば確認できますか? –

+2

https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-entity-see-entityvalidationerrors-propert –

答えて

0

私は100%ではないですが、それはあなたのCarNumberプロパティに問題がある可能性が高いようです。正規表現の属性は文字列プロパティで正常に動作しますが、範囲属性を使用すると、あなたのint型のプロパティの方が良い場合があります。

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")] 
関連する問題