views:

118

answers:

1

I am working on a VB.NET project which is using ASP.NET MVC 2. I am taking advantage of the ability to add validation and other attributes to the metadata in my model.

For example, I have added attributes such as <DisplayName("Full Name")> to the properties in my model and am rendering these using the Html.LabelFor () extension method.

I have also added <Description ("This is a description of the field.")> attributes to various properties in the model and would like to render these in a similar way.

My question is - does an extension method exist which will do this for me?

If there isn't, could someone steer me in the right direction for writing my own? I've started one based on the method signature of the existing methods...

<ExtensionAttribute()> _
Public Function HintFor(Of TModel, TProperty) _
                       (ByVal htmlHelper As HtmlHelper(Of TModel), _
                        ByVal expression As Expression(Of Func(Of TModel, TProperty))) As MvcHtmlString

End Function

But I must admit the 'expression' part of this goes well beyond my Lambda / LINQ / ?? knowledge at this stage!!

Thanks in advance...

+1  A: 

Well, if you're using MVC 3 you can use the DisplayAttribute and just use the Description Parameter like so.

Public Class User
    <Display(Name = "User name", Description = "This is a description")> _
    Public Property Name As String
End Class


<System.Runtime.CompilerServices.Extension> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim attribute = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)

    Return MvcHtmlString.Create(attribute.Description)
End Function

Mvc2

I just ran a cursory test to see if this works. (I don't use VB often and used an online converter) There's no error trapping or anything but it will produce the results you expect.

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString

    Dim ex As MemberExpression = DirectCast(expression.Body, MemberExpression)
    For Each attribute As Attribute In ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(True)
        If GetType(System.ComponentModel.DescriptionAttribute) = attribute.[GetType]() Then
            Return MvcHtmlString.Create(DirectCast(attribute, System.ComponentModel.DescriptionAttribute).Description)
        End If
    Next

    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function

Additional

I'm really not sure why I did the above when you could do it just like this. (though I suppose MVC 2 might not work properly with DataAnnotations)

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function
BuildStarted
Thanks - we'll almost certainly get into MVC 3 at some point soon, but if you have any ideas for MVC 2, that'd be great!?
Chris Roberts
Added an MVC2 version
BuildStarted
Excellent work - many thanks! Works a treat! Don't suppose you know of a good tutorial for this stuff, do you?
Chris Roberts
Heh, your question inspired me to create a blog post similar to this particular entry though utilizing the providers that already exist in mvc. http://buildstarted.com/2010/09/14/creating-your-own-modelmetadataprovider-to-handle-custom-attributes/
BuildStarted
Hello! Your code has been working really well for me - thank you again. Unfortunately, it doesn't appear to work when I add the description attribute to a MetaData class (using the MetadataType attribute on my classes) - any idea how I'd get that going!? :o)
Chris Roberts
Specifically the Description attribute or the description parameter of the Display attribute?
BuildStarted
I'm using the Description attribute at the moment - but I don't really mind so long as I can make it work!! :o) Thanks in advance!
Chris Roberts
Well, first thing I can think of is that you should make sure you're not using the `System.EnterpriseServices` Description attribute. Aside from that I'm going to make an entry that relies on *any* description added to the model - `Display` or `Description` attribute
BuildStarted
Thanks for your continued efforts! Isn't the 'Additional' part of your response the same as your original MVC3 version (which didn't work in MVC2)?
Chris Roberts
heh, yeah...sorry about that. I'm fairly confused as to why it wouldn't work. Try setting a breakpoint on the line with the `If GetType...` and make sure it's being hit and then check the types of each attribute to make sure the `DescriptionAttribute` is being recognized
BuildStarted