次のようなモデルベースのDescriptionForヘルパーがあります。
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());
}
これはDataAnnotations.DisplayAttribute(具体的にはDescriptionパラメータ)をキーオフし、スムーズに動作します。
現在、モデルIEnumerable<foo>にループしている があり、列挙型メンバーごとに s を持つfoo列挙型がある場合があります。DisplayAttributeすべてではありませんが、ほとんどのDisplayAttribute属性がDescriptionプロパティを提供しており、次のようにループで公開したいと考えています。
@foreach(var fooObject in Model.foos){
@Html.Description(fooObject, x=>x.fooEnumVal)
}
...ここに、各fooオブジェクトの列挙値の説明が表示されます。
モデル ヘルパーとあまり似ていないことがすぐにわかりました。誰かがこれについて正しい方向に私を向けることができますか?