2

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
  • mvc 1, 2 or 3? (I assume 1 but thought I should ask) Commented Sep 13, 2010 at 18:06

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

9 Comments

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!?
Excellent work - many thanks! Works a treat! Don't suppose you know of a good tutorial for this stuff, do you?
Heh, your question inspired me to create a blog post similar to this particular entry though utilizing the providers that already exist in mvc. buildstarted.com/2010/09/14/…
Specifically the Description attribute or the description parameter of the Display attribute?
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
|
3

Here is a more complete version I coded for C#, that also supports htmlAttributes:

public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var description = metadata.Description;

    RouteValueDictionary anonymousObjectToHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

    TagBuilder tagBuilder = new TagBuilder("span");
    tagBuilder.MergeAttributes<string, object>(anonymousObjectToHtmlAttributes);
    tagBuilder.SetInnerText(description);

    return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}

Used like this:

[Display(Name = "Style", Description = "Some description of this field")]
public float Style { get; set; }

@Html.DescriptionFor(model => model.Style, new { @class = "help-inline" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.