Ongun23
Ongun23

Reputation: 75

Asp.net Core ViewModel DataAnnotations Localization

I've been practicing on Asp.Net Core MVC recently. We were able to localize and globalise the ViewModel attributes before using below class and overriding. I've been looking for this solution over a week.

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId)
        : base(GetMessageFromResource(resourceId))
    {
    }

    private static string GetMessageFromResource(string resourceId)
    {
        //return value from whatever the source is.
    }
}

My view model looks like

public class LoginViewModel
{
    [CustomRequiredAttribute("Email")]
    [LocalizedDisplayName("Email")]
    [EmailAddress]
    public string Email { get; set; }

    [LocalizedDisplayName("Password")]
    [CustomRequiredAttribute("Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

I copied the same LocalizedDisplay class and tried it on Asp.Net core but didn't work.

The problem is on view, application prints field name not the value from attribute.

Upvotes: 0

Views: 1726

Answers (1)

Ongun23
Ongun23

Reputation: 75

Problem solved : The solation is on githubb https://github.com/dotnet/corefx/issues/11846#issuecomment-248148026

Upvotes: 1

Related Questions