Why do DataAnnotation attributes have difficulty accessing resources created by PublicResxFileCodeGenerator?
I find that the following attribute:
[Compar
That's because you placed your resource file inside the App_GlobalResources
folder which is a special folder in ASP.NET. This should work if you put your resources file somewhere else. This could also be a completely separate project from your ASP.NET MVC application.
Here are the steps you could make this work:
~/Messages.resx
file containing the RegisterModel_ConfirmPasswordError
resource stringSet the custom tool to PublicResXFileCodeGenerator
for this resource file:
Add a model:
public class MyViewModel
{
[Compare("NewPassword",
ErrorMessageResourceName = "RegisterModel_ConfirmPasswordError",
ErrorMessageResourceType = typeof(MvcApplication1.Messages))]
public string Password { get; set; }
public string NewPassword { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
View:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.LabelFor(x => x.Password)
@Html.EditorFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
@Html.LabelFor(x => x.NewPassword)
@Html.EditorFor(x => x.NewPassword)
@Html.ValidationMessageFor(x => x.NewPassword)
}
Then you could start localizing by providing respective translations:
UPDATE:
I was asked in the comments section what's so special about the App_GlobalResources
folder and why it doesn't work with it. Well, actually you could make it work. All you need to do is set the Build Action
to Embedded Resource
. By default when you add a file to the App_GlobalResources
folder, Visual Studio set it to Content
meaning that this resource will not be incorporated into the runtime assembly and ASP.NET MVC cannot find it: