Westwind.Globalization Framework w/ Account.cs ASP.NET MVC

1.8k views Asked by At

I'm having trouble declaring a resource in the Account.CS file. Here's my code:

    [Required(ErrorMessageResourceName= typeof @DbRes.T("Account", "EmailAddress")]
    [Required(ErrorMessageResourceName = (@DbRes.T("EmailAddress", "Account")))]
    [DataType(DataType.EmailAddress)]
    [Display(Name = DbRes.T("EmailAddress", "Account"))]
    public string EmailAddress { get; set; }
2

There are 2 answers

0
Rick Strahl On BEST ANSWER

You can definitely use Westwind.Globalization with MVC and on validation attributes. MVC validation relies on strongly typed resources and type mapping so in order to get this to work you have export strongly typed resources from your dbResource file. This will create a custom strongly typed class for each ResourceSet in your db resources.

A strongly typed resource class looks like this:

public class Resources
{
    public static System.String NameIsRequired
    {
        get
        {
            if (GeneratedResourceSettings.ResourceAccessMode == ResourceAccessMode.AspNetResourceProvider)
                return (System.String) HttpContext.GetGlobalResourceObject("Resources","NameIsRequired");
            if (GeneratedResourceSettings.ResourceAccessMode == ResourceAccessMode.Resx)
                return ResourceManager.GetString("NameIsRequired");

            return DbRes.T("NameIsRequired","Resources");
        }
    }


    public static System.String AddressIsRequired
    {
        get
        {
            if (GeneratedResourceSettings.ResourceAccessMode == ResourceAccessMode.AspNetResourceProvider)
                return (System.String) HttpContext.GetGlobalResourceObject("Resources","AddressIsRequired");
            if (GeneratedResourceSettings.ResourceAccessMode == ResourceAccessMode.Resx)
                return ResourceManager.GetString("AddressIsRequired");

            return DbRes.T("AddressIsRequired","Resources");
        }
    }

    ... any others in the same resource set
}

This is very similar to standard .Resx strongly typed classes, except that you can access various different kinds of resources using the same class (DbRes ResourceManager, dbRes ASP.NET ResourceProvider or plain Resx.

You can then reference the strongly typed resources the same way you would reference standard Resx strongly typed resources in your ASP.NET MVC model validation attributes:

public class ViewModelWithLocalizedAttributes
{
    [Required(ErrorMessageResourceName = "NameIsRequired", ErrorMessageResourceType = typeof(Resources))]
    public string Name { get; set; }

    [Required(ErrorMessageResourceName = "AddressIsRequired", ErrorMessageResourceType = typeof(Resources))]
    public string Address { get; set; }
}

There's an example in the sample project - open the Models folder to see the model and ModelAttributesFromResources.cshtml view that uses that view with localized values.

There's a topic on the wiki that describes this as well:

https://github.com/RickStrahl/Westwind.Globalization/wiki/Model-Validation-Message-for-ASP.NET-and-EntityFramework

1
devmb On

The Westwind Documentation: Westwind.Globalization ASP.NET MVC Support says:

MVC applications work best by NOT using the ASP.NET resource architecture of App_LocalResources and App_GlobalResources. Because MVC doesn't use controls any of the control lookup benefits of declarative markup that the Web Forms based provider system provides is lost in MVC applications. Instead MVC applications tend to work best with traditional resources defined in your .NET projects and exposed as strongly typed resources.

So it seems like Westwind got no solution, except making RESX-File editing maybe easier.

//EDIT: See Ricks comment: [...]You can generate strongly typed resources from the db resources and then use those the same way. [...] There's a seperate topic on the the Wiki.