I have a model class that uses a DisplayAttribute DataAnnotation. I’m trying to retrieve information in that DisplayAttribute in Mercury, similar to this StackOverflow example. But I get a compile error. Am I doing something wrong, or is it Mercury?
Here is my Extensions class:
Public Class Extensions
Public Shared Function GetDescription(Of T As Class)(ByVal propertyName As String) As String
Dim memberInfo As MemberInfo = GetType(T).GetProperty(propertyName)
If memberInfo Is Nothing Then
Return Nothing
End If
Return memberInfo.GetCustomAttribute(Of DisplayAttribute)()?.GetDescription()
End Function
End Class
Here is how it looks on my Razor Page: <input type="text" asp-for="CurrentToDo.Name" placeholder="Name" title='@Extensions.GetDescription(Of ToDoModel)("Name")' />
I’ve also tried it this way, per the solution comment: <input type="text" asp-for="CurrentToDo.Name" placeholder="Name" title='(@Extensions.GetDescription(Of ToDoModel)("Name"))' />
The error I get in either case is “comma or closing parenthesis expected”:
Also, as an aside: The “Paste & Convert C# Code” for the Shared Function did not handle this code very well. Here’s the result, which swallowed the “(Of T As Class)” part and also split the return line into multiple lines that aren’t quite right:
Public Shared Class Extensions
Public Function GetDescription(propertyName As String) As String
Dim memberInfo As MemberInfo = GetType(T).GetProperty(propertyName)
If (memberInfo) Is Null Then
Return Null
End If
If memberInfo.GetCustomAttribute(Of DisplayAttribute)() IsNot Nothing Then
GetDescription()
result = memberInfo.GetCustomAttribute(Of DisplayAttribute)()
End If
Return
End Function
End Class
However, I’m still having trouble getting the syntax right. I’ve tried this: <input type="text" asp-for="CurrentToDo.Name" placeholder="Name" title='(@Extensions.GetDescription<ToDoModel>("Name"))' />
and this: <input type="text" asp-for="CurrentToDo.Name" placeholder="Name" title='@Extensions.GetDescription<ToDoModel>("Name")' />
But I still get the same error. Maybe I’m not implementing the C# syntax properly?
FWIW, slightly tangential, i have also hooked up the option to toggle between Original and Generated file in Fire/Water now, for Razor (which was a tad bit more complicated than for other code-behind cases, for reasons):
Hmmmmm…you are right, it does compile without that, but it doesn’t work. It is supposed to make the tool the description of the Display attribute for that property, but it is not.
Ok, but are you sure that supported in these HTML attributes? it seems they ALREADY get treated as C#, even without the @. Also, wouldn’t it need to be outside of the (), at least?
I gotta admit i don;'t know much about how this stuff works, but the cshtml-to-cs conversion is all handled Microsoft’s tool and out of our control…
That said, without the @ it does generate the code i would expect?/
However, the result isn’t rendering on the page. No errors - it just doesn’t work.
So I tried creating a C# test project to test out the original StackOverflow sample I was using, and I can’t get that to work either. So this whole approach was based on some sample code that apparently doesn’t work.
Oh well, I learned some things in the process! But sorry to take you down this winding road just to come back to square one.
Granted, although I’ve generally had good luck with solutions I find there. Might not be the BEST solution, but typically it works. Then again, I’ve been living a long time in the VB.NET and ASP.NET WebForms world, where things don’t change that much, unlike the constant churn of this .NET Core shi…ummm STUFF.