I've seen other people reported this problem, but it appers there is no a solution. Here is how to reproduce the problem (I also have a repro project)
1. Create an empty asp.net mvc 3 app with razaor
2. Add global resource named Resource and create one resource string called SomeText with some text.
3. Modify LogOnModel model (under Model, AccountModel.cs)
public class LogOnModel
{
[Required (ErrorMessageResourceName="SomeText", ErrorMessageResourceType=typeof(Resources.Resource))]
[Display(Name = "User name")]
public string UserName { get; set; }
..
4. Run the app, click on Log On, click OK with emty fields. Note there is validation message that is coming from the resouce5. Now modify the code:
public class LogOnModel
{
[Required (ErrorMessageResourceName="SomeText", ErrorMessageResourceType=typeof(Resources.Resource))]
//[Display(Name = "User name")]
[Display (Name="SomeText", ResourceType=typeof(Resources.Resource))]
public string UserName { get; set; }
This should pull the Display name from the resource and we know that the resource is good, it worked for validation. So it should work, right? In fact it blows up with "Cannot retrieve property 'Name' because localization failed. Type 'Resources.Resource'
is not public or does not contain a public static string property with the name 'SomeText'" The class Resource is generated as internal, I understand that, but why it works for validation? I saw people suggesting changing class to public to internal. Seem
like not a big deal. Suppose I can change resource generator (or simply replace internal to public just for test). This, however also bombs - complains about class being declared in two places.. Is this a bug or I am doing something wrong? -Stan
I think that you should start by renaming the Resource-file. "Resource" could conflict if you put it in some bad place. Name it "TextResources" or something more unique.
Really not sure about what happens though, i have had no problems using this, except i have a special attribute for the same feature.
* REMEMBER TO MARK THE ANSWER TO YOUR QUESTION * .NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
I don't belive renaming resource file has anything to do with it. For some reason framework pulls resource for one property and doesn't for another. It is very reprosusible..
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Zero dice - "Cannot retrieve property 'Name' because localization failed. Type 'Resources.Resource' is not public or does not contain a public static string property with the name 'SomeText'."
What this mean is that Display property of annotation is not localizable. As soon as you try
I have this kind of functionality working perfectly, although i have my resource-files in another assembly which naturally forces me to have them publich. But anyway I'm using the same approach:
[Display(ResourceType = typeof(PropertyTexts), Name = "PresentationText")]
And the PropertyTexts is just a simple resource files with Access Modifier: Public.
So, are you sure there is not conflict about that Resource file? (i recall you hade some errormessage saying something about class beeing declared in two places.
* REMEMBER TO MARK THE ANSWER TO YOUR QUESTION * .NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
I have just dig into code and found that this is not specific to asp.net mvc. This is the problem with System.ComponentModel.DataAnnotations attributes. DisplayAttribute checks the
Type.IsVisible
property while ValidationAttribute(RequiredAttribute inherit from them) will not. So you need to change your resource class and properties to public to make it work.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by StanB on May 31, 2011 12:37 PM
That explains why one property works and another doesn't! Thank you.
Here comes second part - changing to class from internal to public. If I change visiblity of the class to public in code-generated file (I understand that the file will regenerated after resource changes, but just to try) I get an error message during run
time saying that class is defined in two places, one place is asp.net\Temporary Files and another one is the assembly.. I could never figure out why is that...
Try clean solution and rebuild and see if that solved the "duplicate" error. Otherwise you might consider my first suggestion to rename the resource file
* REMEMBER TO MARK THE ANSWER TO YOUR QUESTION * .NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
To summarize here is what resolves the issue, in case anybody needs it.
For the global resource file:
1. Change custom tool to "PublicResXFileCodeGenerator"
2. Change Build Action to EmbeddedResource
3. This is optional. Change custom tool namespace to "Resources". This will make generated class compatible with the class generated by default GlobalResourceProxyGenerator and won't break existing code, if you have one.
StanB
Member
69 Points
35 Posts
Model localization Display name - I belive it is a bug...
May 29, 2011 05:47 PM|LINK
I've seen other people reported this problem, but it appers there is no a solution. Here is how to reproduce the problem (I also have a repro project)
1. Create an empty asp.net mvc 3 app with razaor
2. Add global resource named Resource and create one resource string called SomeText with some text.
3. Modify LogOnModel model (under Model, AccountModel.cs)
public class LogOnModel { [Required (ErrorMessageResourceName="SomeText", ErrorMessageResourceType=typeof(Resources.Resource))] [Display(Name = "User name")] public string UserName { get; set; } ..4. Run the app, click on Log On, click OK with emty fields. Note there is validation message that is coming from the resouce5. Now modify the code:
public class LogOnModel { [Required (ErrorMessageResourceName="SomeText", ErrorMessageResourceType=typeof(Resources.Resource))] //[Display(Name = "User name")] [Display (Name="SomeText", ResourceType=typeof(Resources.Resource))] public string UserName { get; set; }This should pull the Display name from the resource and we know that the resource is good, it worked for validation. So it should work, right? In fact it blows up with "Cannot retrieve property 'Name' because localization failed. Type 'Resources.Resource' is not public or does not contain a public static string property with the name 'SomeText'" The class Resource is generated as internal, I understand that, but why it works for validation? I saw people suggesting changing class to public to internal. Seem like not a big deal. Suppose I can change resource generator (or simply replace internal to public just for test). This, however also bombs - complains about class being declared in two places.. Is this a bug or I am doing something wrong? -Stan
Knecke
Contributor
3712 Points
838 Posts
Re: Model localization Display name - I belive it is a bug...
May 29, 2011 06:43 PM|LINK
I think that you should start by renaming the Resource-file. "Resource" could conflict if you put it in some bad place. Name it "TextResources" or something more unique.
Really not sure about what happens though, i have had no problems using this, except i have a special attribute for the same feature.
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
StanB
Member
69 Points
35 Posts
Re: Model localization Display name - I belive it is a bug...
May 29, 2011 07:13 PM|LINK
I don't belive renaming resource file has anything to do with it. For some reason framework pulls resource for one property and doesn't for another. It is very reprosusible..
-Stan
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Model localization Display name - I belive it is a bug...
May 30, 2011 06:41 AM|LINK
Try to set Build Action as Embedded Resource,
http://forums.asp.net/p/1591840/4035071.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
StanB
Member
69 Points
35 Posts
Re: Model localization Display name - I belive it is a bug...
May 30, 2011 10:39 PM|LINK
Zero dice - "Cannot retrieve property 'Name' because localization failed. Type 'Resources.Resource' is not public or does not contain a public static string property with the name 'SomeText'."
What this mean is that Display property of annotation is not localizable. As soon as you try
It bombs, but another attribute in the same model class with the same resource works:
How can one attribute work and another doesn't?
Knecke
Contributor
3712 Points
838 Posts
Re: Model localization Display name - I belive it is a bug...
May 31, 2011 05:05 AM|LINK
I have this kind of functionality working perfectly, although i have my resource-files in another assembly which naturally forces me to have them publich. But anyway I'm using the same approach:
And the PropertyTexts is just a simple resource files with Access Modifier: Public.
So, are you sure there is not conflict about that Resource file? (i recall you hade some errormessage saying something about class beeing declared in two places.
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Model localization Display name - I belive it is a bug...
May 31, 2011 06:09 AM|LINK
I have just dig into code and found that this is not specific to asp.net mvc. This is the problem with System.ComponentModel.DataAnnotations attributes. DisplayAttribute checks the Type.IsVisible property while ValidationAttribute(RequiredAttribute inherit from them) will not. So you need to change your resource class and properties to public to make it work.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
StanB
Member
69 Points
35 Posts
Re: Model localization Display name - I belive it is a bug...
May 31, 2011 12:37 PM|LINK
That explains why one property works and another doesn't! Thank you.
Here comes second part - changing to class from internal to public. If I change visiblity of the class to public in code-generated file (I understand that the file will regenerated after resource changes, but just to try) I get an error message during run time saying that class is defined in two places, one place is asp.net\Temporary Files and another one is the assembly.. I could never figure out why is that...
Knecke
Contributor
3712 Points
838 Posts
Re: Model localization Display name - I belive it is a bug...
May 31, 2011 12:42 PM|LINK
This is what we told you all along :)
Try clean solution and rebuild and see if that solved the "duplicate" error. Otherwise you might consider my first suggestion to rename the resource file
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
StanB
Member
69 Points
35 Posts
Re: Model localization Display name - I belive it is a bug...
May 31, 2011 02:25 PM|LINK
I found the answer in http://holyhoehle.wordpress.com/2010/02/20/making-global-resources-public/
To summarize here is what resolves the issue, in case anybody needs it.
For the global resource file:
1. Change custom tool to "PublicResXFileCodeGenerator"
2. Change Build Action to EmbeddedResource
3. This is optional. Change custom tool namespace to "Resources". This will make generated class compatible with the class generated by default GlobalResourceProxyGenerator and won't break existing code, if you have one.
-Stan