【问题标题】:Required Atrribute, how to load error message from database? MVC必需属性,如何从数据库加载错误消息? MVC
【发布时间】:2015-01-07 15:51:52
【问题描述】:

如何使用数据库中的错误消息字符串作为必需的错误消息?

对于必需的验证属性,在我的地址模型中,我想使用存储在我的数据库中的错误消息,而不是对其进行硬编码。它来自数据库而不是 resx 文件。

在我当前的代码中,我使用 SSPResourceManager 类根据 ErrorMessageResourceName 调用数据库。由于依赖注入的原因,这不起作用。有没有其他方法。

当前代码

型号

public abstract class AddressVM
{
    [Required(ErrorMessageResourceType = typeof(SSPResourceManager), ErrorMessageResourceName = "SSP_Validation_Required")]
    [StringLength(50)]
    public string Name { get; set; }
}

我在这里从 SSPResourceManager 类中寻找静态属性“SSP_Validation_Required”

SSPResourceManager 类

 public class SSPResourceManager
 {
    private static ITranslationService _translationService = EnterpriseLibraryContainer.Current.GetInstance<ITranslationService>();

    private static string _languageCode = System.Web.HttpContext.Current.Session["LanguageCode"].ToString();

    public static string SSP_Validation_Required
    {
        get
        {
             // call database and retrive the correct error string 
            return _translationService.Read("SSP_Validation_Required", "SSP", _languageCode); 
        }
    }
}

这种方法不起作用,因为我在注入 Itranslation 依赖项时遇到问题。

是否有另一种方法可以从数据库加载错误消息并在所需属性中使用?

【问题讨论】:

  • 不幸的是,这并不容易或直截了当。默认的 DataAnnotationValidationProvider 仅支持资源文件或静态类。我可以在这里为您找到解决方案:geekswithblogs.net/shaunxu/archive/2012/09/04/…,但就像我说的,您需要为自己完成一些工作。

标签: asp.net-mvc


【解决方案1】:

解决它的一种方法是创建一个基于RequiredAttribute 的新类

public class ResourceErrorMessage : RequiredAttribute
{
     public ResourceErrorMessage(string resourceKey): base()
     {
           this.ErrorMessage = DependencyResolver.Current.GetService<ITranslationService>().Translate(ResourceKey, Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);
     }  
}

基于DataAnnotationsModelMetadataProvider创建CustomMetadataProvider

 public class CustomMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(
                                 IEnumerable<Attribute> attributes,
                                 Type containerType,
                                 Func<object> modelAccessor,
                                 Type modelType,
                                 string propertyName)
            {
                var data = base.CreateMetadata(attributes,
                                          containerType,
                                         modelAccessor,
                                         modelType,
                                         propertyName);

                var resourceErrorMessage = attributes
                            .SingleOrDefault(a => typeof(ResourceErrorMessage) == a.GetType());
                if (resourceErrorMessage != null) data.AdditionalValues
                            .Add("ResourceErrorMessage", ((ResourceErrorMessage)resourceErrorMessage).ResourceKey);

                return data;
            }
        }

global.asax Application_Start() 中添加 ModelMetadataProviders.Current = new CustomMetadataProvider(); 并在你的模型上使用它

public class TestModel
{
  [ResourceErrorMessage("somestring")]
  public int TextAttribute{ set; get; }
}

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    相关资源
    最近更新 更多