Reputation: 18802
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...
Upvotes: 2
Views: 4214
Reputation: 1670
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" })
Upvotes: 3
Reputation: 26689
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
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
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
Upvotes: 4