c# - 显示不带标签的显示名称属性的 Html 帮助程序

标签 c# html asp.net-mvc html-helper

我有这个:

[Display(Name = "Empresa")]
public string Company{ get; set; }

在我的 aspx 中我有:

<th><%: Html.LabelFor(model => model.Company)%></th>

这会产生:

<th><label for="Company">Empresa</label></th>

是否有任何 html 辅助扩展只显示没有标签的显示属性,只有纯文本?我想要的输出是这样的:

<th>Empresa</th>

谢谢!

编辑

我按照建议尝试了 DisplayFor 或 DisplayTextFor,但它们无效,因为它们生成:

<th>Amazon</th> 

它们返回属性的值...我想要 Display 属性中的名称。

最佳答案

后期编辑

在 >= MVC4 中,只需使用 @Html.DisplayNameFor

在那之前

使用你自己的助手

public static MvcHtmlString SimpleLabelFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression
) {
  var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
  string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
  string resolvedLabelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
  if (String.IsNullOrEmpty(resolvedLabelText)) 
            return MvcHtmlString.Empty;

  return MvcHtmlString.Create(resolvedLabelText);

}

关于c# - 显示不带标签的显示名称属性的 Html 帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447801/

相关文章:

c# - 如何在 .Net 上创建 1px X 1px

c# - 具有 lambda 表达式的对象列表中的累积和 C#

javascript - jquery ajax $(this) 对象从 .html() 返回 null

html - IE 不会将 RTL 段落环绕在 float 元素周围

javascript - 如何将 javascript 值分配给临时数据

c# - 嵌套的Json反序列化c#

c# - 如何在 c# asp html 中制作具有 tiles 样式的树?

javascript - CKEditor 改变了我所有的 HTML

asp.net-mvc - 添加 MVC Controller 或 View 时的 Visual Studio 2015 "Not Responding"

asp.net-mvc - ASP.NET MVC - 如何解释?