由DataAnnotations验证引起的ArgumentNullException

本文关键字:ArgumentNullException DataAnnotations 验证 | 更新日期: 2023-09-27 18:12:38

我有一个绑定到视图的模型。我试图添加RegEx属性到某些输入字段。如果我这样做,它会给我这个:

EventName="MessageEvent" Message="Unhandled ArgumentNullException in Papaya.Web.Controllers.CompanyController Update
ArgumentNullException: Value cannot be null.
Parameter name: value
at
at  System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value)

如果没有设置regulareexpression属性,它可以正常工作。模型如下:

[Required(ErrorMessageResourceName = nameof(Company_Resources.RequiredName), ErrorMessageResourceType = typeof(Company_Resources))]
[RegularExpression(Helpers.InputValidationStringLetters, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringLetters), ErrorMessageResourceType = typeof(Common_Resources))]
[Display(Name = nameof(Company_Resources.NameLbl), ResourceType = typeof(Company_Resources))]
public virtual string Name { get; set; }
[CorporateIdentityNumber(ErrorMessageResourceName = nameof(Customer_Resources.vmsgInvalidCorporateIdentityNumber), ErrorMessageResourceType = typeof(Customer_Resources))]
    [RegularExpression(Helpers.InputValidationStringNumbers, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringNumbers), ErrorMessageResourceType = typeof(Common_Resources))]
    [Display(Name = nameof(Company_Resources.CompanyRegSocialSecurityNoLbl), ResourceType = typeof(Company_Resources))]
    public virtual string CompanyRegSocialSecurityNo { get; set; }
    [Display(Name = nameof(Company_Resources.lblAddress1), ResourceType = typeof(Company_Resources))]
   [RegularExpression(Helpers.InputValidationStringLettersNumbersDots, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringLettersNumbersDots), ErrorMessageResourceType = typeof(Common_Resources))]
     public virtual string Address1 { get; set; }
    [Display(Name = nameof(Company_Resources.lblAddress2), ResourceType = typeof(Company_Resources))]
    [RegularExpression(Helpers.InputValidationStringLettersNumbersDots, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringLettersNumbersDots), ErrorMessageResourceType = typeof(Common_Resources))]
    public virtual string Address2 { get; set; }
    [Display(Name = nameof(Company_Resources.ZipCodeLbl), ResourceType = typeof(Company_Resources))]
    [RegularExpression(Helpers.InputValidationStringNumbers, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringNumbers), ErrorMessageResourceType = typeof(Common_Resources))]
    public virtual string ZipCode { get; set; }
    [Display(Name = nameof(Company_Resources.CityLbl), ResourceType = typeof(Company_Resources))]
    [RegularExpression(Helpers.InputValidationStringLetters, ErrorMessageResourceName = nameof(Common_Resources.emsgInputValidationStringLetters), ErrorMessageResourceType = typeof(Common_Resources))]
    public virtual string City { get; set; }
    [Display(Name = nameof(Company_Resources.PhoneLbl), ResourceType = typeof(Company_Resources))]
    public virtual string Phone { get; set; }

我错过了什么吗?

编辑:在一个显示值上,我使用了资源中的字符串,该字符串为空,因此出现了错误。谢谢你们了!

由DataAnnotations验证引起的ArgumentNullException

尝试下面的正则表达式:

[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
     string TextBoxData {
        get;
        set;
    }

并应用如下:

@Html.EditorFor(model => model.TextBoxData)
@Html.ValidationMessageFor(model => model.TextBoxData )
相关文章:
  • 没有找到相关文章