简体   繁体   中英

MVC3 C# LocalizedRequiredAttribute not works

I wrote this attribute extension:

public class LocalizedRequiredAttribute : RequiredAttribute
{
    public LocalizedRequiredAttribute(string resourceTag)
    {
        ErrorMessage = GetMessageFromResource(resourceTag);
    }

    private static String GetMessageFromResource(String resourceTag)
    {
        return ResourceManager.Current.GetResourceString(resourceTag);
    }
}

but I get this error: 'System.Resources.ResourceManager' does not contain a definition for 'Current'.

What is wrong?

Thanks a lot.

This can already be done using the RequiredAttribute

[Required(ErrorMessageResourceType=typeof(ClassLib1.Resources), ErrorMessageResourceName="Character_FirstName_Required")]

see Model Metadata and Validation Localization using Conventions


EDIT: I Guess you're referencing the wrong ResourceManager, see ResourceManager.Current | current property

看起来您在定义此属性的文件中没有引用Windows.ApplicationModel.Resources.Core命名空间,因此VS明智地引用了System.Resources.Resource管理器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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