2017-12-23 13 views
-1

asp.net (visual studio 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

그래서, 그것은 무엇을 말해 주는가? –

+0

어떻게 확인할 수 있습니까? –

+2

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

답변

0

나는 100 % 아니에요 그러나 당신의 CarNumber 속성에 문제가 될 가능성이 보인다. 으로 RegularExpression은 INT 속성에 더 좋을 수있는 범위 속성을 사용하여 문자열 속성에 잘 작동하지만 것이다 속성 : 당신이 EntityValidationErrors 속성을 확인하면

[Range(0, int.MaxValue, ErrorMessage = "Please enter valid integer Number")]