【问题标题】:How to specify the Id in LabelFor Method如何在 LabelFor 方法中指定 Id
【发布时间】:2014-02-26 07:59:46
【问题描述】:

我在 Mvc 中有 LabelFor 像

@Html.LabelFor(model => model.EmployeeName)

创建自定义 LableFor 以赋予标题等.. 我创建了自己的喜欢

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string Title = null)

给我打电话的标题

@Html.LabelFor(model => model.EmployeeAdress,"This is Employee ADress")

现在正确且工作正常

现在我已经采取了 500 到 600 个这样的地点

@Html.LabelFor(model => model.EmployeeName)

我不想在这里硬编码标题

@Html.LabelFor(model => model.EmployeeName,"EmployeeName")

只要它找到这个我就想要它

@Html.LabelFor(model => model.EmployeeName)

然后我的自定义方法应该调用,即 public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, string Title = null)

所以我不需要更改整个应用程序,而是调用默认的 Mvc 方法

【问题讨论】:

    标签: asp.net-mvc html-helper


    【解决方案1】:

    LabelFor() 是一种扩展方法(静态),因此不能被覆盖。您必须创建自己的 Html Helper Extension 方法来实现您所需要的。将您的自定义标签 htmlhelper 重命名为 LabelForCustom(或您使用的任何名称)并用这个新的扩展方法替换所有 @Html.LabelFor(...)

    【讨论】:

      【解决方案2】:

      为此目的使用DisplayAttributeDisplayNameAttributeSee this link about DisplayAttribute Class

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多