我有一个绑定到视图的模型。我试图将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)
如果未设置RegularExpresion属性,则可以正常工作。这是模型:
[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; }
我错过了什么吗?
编辑: 在一个显示值上,我使用了资源中的字符串,该字符串为null,因此是错误。多谢你们!
答案 0 :(得分:0)
尝试使用以下正则表达式仅允许字母:
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
string TextBoxData {
get;
set;
}
并应用如下:
@Html.EditorFor(model => model.TextBoxData)
@Html.ValidationMessageFor(model => model.TextBoxData )