Answered by:
How do I specify an error message from a resource file for the VB data annotation <Required>?

Question
-
User-404502133 posted
Hi,
VS2013, VB, MVC 5
This is part of my Class code:
Public Class Order ... Private mFirstName As String <Required> Public Property FirstName() As String Get Return mFirstName End Get Set(ByVal value As String) mFirstName = value End Set End Property ...
I want to setup a resource file to allow country changes for error messages and the first attribute I am working with is <Required>. What would I write to have the error message pulled from a resource file named ErrorMessages.resx?
Broader guidance to find a location where I can understand the syntax for all of the VB data annotations would be helpful.
Thanks.
Best Regards,
Alan
Monday, September 8, 2014 3:45 PM
Answers
-
User-404502133 posted
So, I managed to find what needed to be written and I show it below:
<Required(ErrorMessageResourceName:="FirstNameRequired", ErrorMessageResourceType:=GetType(Resources.ErrorMessages))>
Figuring this out was brutal. I really just stumbled onto an obscure web page from 2005 where someone was explaining the VB.NET equivalent of the C# typeof() operator. None of the more recent explanations of VB's "TypeOf() IS <whatever>" seem even remotely close to the answer above. Is it just the case I let the Visual Studio error messages push me in a wrong direction?
If anyone has any counsel or guidance regarding the specific syntax for <Required> it would surely be appreciated, but since I found the solution to this specific question...
I would like to also ask for counsel or guidance on how I might 'get more targeted' when searching for proper syntax for various VB.NET operators, especially as it relates to asp.net and MVC 5 programming. The web page for the <Required> attribute (MSDN page) didn't seem to point me anywhere near the solution above, which leaves me wondering how I should actually go about finding answers for the more arcane solutions in VB.
Best Regards,
Alan
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 8, 2014 5:38 PM
All replies
-
User-404502133 posted
Adding to my post above, it seems to use a resource file I must set the following properties for the <Required> attribute:
ErrorMessageResourceName (key to lookup the error message Name string in my resource file)
ErrorMessageResourceType (type for the resource file, where Visual Studio automatically created Resources.ErrorMessages as seen below)
I took the above from: MSDN page
So I wrote the following line, but don't understand the syntax and what it does at the end where you can see '????':
<Required(ErrorMessageResourceName:="FirstNameRequired", ErrorMessageResourceType:=TypeOf (Resources.ErrorMessages) Is ????)>
The C# equivalent for this seems to be:
[Required(ErrorMessageResourceType=typeof(ErrorMessages),ErrorMessageResourceName="FirstNameRequired")]
Note: The order of the properties were purposely reversed in this above.
Best Regards,
Alan
Monday, September 8, 2014 5:10 PM -
User-404502133 posted
So, I managed to find what needed to be written and I show it below:
<Required(ErrorMessageResourceName:="FirstNameRequired", ErrorMessageResourceType:=GetType(Resources.ErrorMessages))>
Figuring this out was brutal. I really just stumbled onto an obscure web page from 2005 where someone was explaining the VB.NET equivalent of the C# typeof() operator. None of the more recent explanations of VB's "TypeOf() IS <whatever>" seem even remotely close to the answer above. Is it just the case I let the Visual Studio error messages push me in a wrong direction?
If anyone has any counsel or guidance regarding the specific syntax for <Required> it would surely be appreciated, but since I found the solution to this specific question...
I would like to also ask for counsel or guidance on how I might 'get more targeted' when searching for proper syntax for various VB.NET operators, especially as it relates to asp.net and MVC 5 programming. The web page for the <Required> attribute (MSDN page) didn't seem to point me anywhere near the solution above, which leaves me wondering how I should actually go about finding answers for the more arcane solutions in VB.
Best Regards,
Alan
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 8, 2014 5:38 PM