本文介绍了RequiredAttribute标签派生不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现我自己的RequiredAttribute标签,我在其中调用自定义的资源处理程序:
I'm trying to implement my own RequiredAttribute, in which I call a custom resource handler:
public class LocalizedValidationAttributes
{
public class LocalizedRequiredAttribute : RequiredAttribute
{
private String _resourceString = String.Empty;
public new String ErrorMessage
{
get { return _resourceString; }
set { _resourceString = GetMessageFromResource(value); }
}
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
我把这个方式如下:
I call this the following way:
[LocalizedValidationAttributes.LocalizedRequiredAttribute(ErrorMessage = "test")]
public String Text { get; set; }
不过的ErrorMessage的吸气剂是永远不会被调用。
But the getter of ErrorMessage is never called.
任何提示?谢谢!
推荐答案
尝试这样的:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return ResourceManager.Current.GetResourceString(name);
}
}
或者是这样的:
or like this:
public class LocalizedRequiredAttribute : RequiredAttribute
{
public LocalizedRequiredAttribute(string resourceTag)
{
ErrorMessage = GetMessageFromResource(resourceTag);
}
private static String GetMessageFromResource(String resourceTag)
{
return ResourceManager.Current.GetResourceString(resourceTag);
}
}
然后:
[LocalizedValidationAttributes.LocalizedRequiredAttribute("test")]
public String Text { get; set; }
这篇关于RequiredAttribute标签派生不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!