I have a model-based DescriptionFor
helper that looks like this:
public static HtmlString DescriptionFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression) where TModel : class
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
return new HtmlString(metaData.Description.ToStringOrEmpty());
}
This keys off the DataAnnotations.DisplayAttribute
(specifically, the Description
parameter) and it works swimmingly.
I have a case now where my model has an IEnumerable<foo>
that I am looping through, and foo
has an enum with DisplayAttribute
s for each enum member. Most, but not all, of the DisplayAttribute
attributes have provided a Description
property, and I want to expose that in my loop like so:
@foreach(var fooObject in Model.foos){
@Html.Description(fooObject, x=>x.fooEnumVal)
}
...where this would display the enum value description for each foo
object.
I quickly discovered that it was not very similar to the model helper. Can someone point me in the right direction for this?