Asp.net MVC 服务端验证多语言错误

服务端验证用户提交信息时为了实现多语言使用了资源文件,如:

using System.ComponentModel.DataAnnotations;

public class UserModel
{

    [Display(ResourceType =typeof(AppStrings),Name ="Field_User_Email")]
    [Required(ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_Required")]
    [EmailAddress(ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_Format")]
    [StringLength(100, ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_MaxLength")]
    public string Email { get; set; }

    //...
}

测试时出现如下错误:

Cannot retrieve property 'Name' because localization failed.  Type 'Resources.AppStrings' is not public or does not contain a public static string property with the name 'Field_User_Email'.

 解决方法:

设置资源文件的属性,将Custom Tool改为"PublicResXFileCodeGenerator"

 

posted @ 2018-12-20 09:25  绿皮儿橘子  阅读(449)  评论(0编辑  收藏  举报