简体   繁体   English

如何从返回字符串的静态方法设置数据注释ErrorMessage字符串值

[英]How to set data annotation ErrorMessage string value from static method that returns string

I am using global static method for localization purpose. 我使用全局静态方法进行本地化。 This static method takes Resource Name and returns string Resource Value for it. 此静态方法获取资源名称并为其返回字符串资源值。 This method is like this 这个方法是这样的

public static string GetResourceValue(string ResourceName)
{
    return "StringValue";
}

I am accessing this method from my View like this 我正在从我的View中访问此方法

@MyClass.GetResourceValue("ResourceName")

and it returns string value to display. 它返回显示的字符串值。 This works properly but how do I use this same method to use in my Model? 这工作正常,但如何在我的模型中使用相同的方法? Is there any way to set ErrorMessage value from this static method some thing like this 有没有办法从这个静态方法设置ErrorMessage值这样的事情

[Required(ErrorMessage=MyClass.GetResourceValue("ResourceName"))]
public string Name { get; set; }

I got solution to this problem using this link 我使用此链接解决了这个问题

I just created new class with static properties like this 我刚刚用这样的静态属性创建了新类

public class LocalizedErrorMsg
{
    public static string NameRequiredMsg
    {
        get
        {
            return MyClass.GetResourceValue("NameRequiredMsg");
        }
    }
}

and in data annotation passed ErrorMessageResourceName as static property name and ErrorMessageResourceType as class name like this 并在数据注释中将ErrorMessageResourceName作为静态属性名称传递,并将ErrorMessageResourceType作为类名称传递给

[Required(ErrorMessageResourceName = "NameRequiredMsg", ErrorMessageResourceType = typeof(LocalizedErrorMsg))]
public string Name { get; set; }

problem is that it need to create extra class that stores static properties which returns error message from my localization global static method. 问题是它需要创建额外的类来存储静态属性,这些属性从我的本地化全局静态方法返回错误消息。 I searched but I didn't get solution other than this. 我搜索了但除此之外我没有得到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM