たぶん誰もが思いつくであろう、DisplayName属性の値を表示するHtmlHelperの拡張メソッドです。
どうして標準でないんだろう。あれ、知らないだけなのか...?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class HtmlHelperExtensions | |
{ | |
public static MvcHtmlString DisplayNameFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) | |
{ | |
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); | |
var value = | |
metadata.DisplayName ?? ( | |
metadata.PropertyName ?? | |
ExpressionHelper.GetExpressionText(expression) | |
); | |
return MvcHtmlString.Create(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="display-label"> | |
<%= Html.DisplayNameFor(m => m.Hoge) %> | |
</div> | |
<div class="display-field"> | |
<%= Html.DisplayFor(m => m.Hoge) %> | |
</div> |
0 件のコメント:
コメントを投稿