http://weblogs.asp.net/imranbaloch/archive/2010/07/03/asp-net-mvc-labelfor-helper-with-htmlattributes.aspx에서 많이 빌리 자이 작업을 수행하는 확장 방법을 만들었습니다. 항상 안전한 span 태그로 출력합니다. span 태그를 완전히 생략하여이 경우 속성을 사용할 수 없기 때문에 두 개의 오버로드를 제거하도록 수정할 수도 있습니다.
는 페이지가 다음 Html.DisplayNameFor(x => x.Name)
public static class DisplayNameForHelper
{
public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return DisplayNameFor(html, expression, new RouteValueDictionary());
}
public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return DisplayNameFor(html, expression, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString DisplayNameFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("span");
tag.MergeAttributes(htmlAttributes);
tag.SetInnerText(labelText);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
}
감사 대린처럼보기에이를 사용하는 클래스의 네임 스페이스를 가져 확인이 콘텐츠와 클래스를 만듭니다! 그게 내가 필요한거야. – kseen