Html 提取用于标签元素的值?

Html 提取用于标签元素的值?,html,asp.net-mvc-2,label,html-helper,Html,Asp.net Mvc 2,Label,Html Helper,嗨 标签具有指向编辑器的for属性,例如: <label for="ModelViewAd_Title">Titel</label> 我正在构建用于生成标签的自定义帮助器,此methodhead如下所示: public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>

标签具有指向编辑器的for属性,例如:

<label for="ModelViewAd_Title">Titel</label>
我正在构建用于生成标签的自定义帮助器,此methodhead如下所示:

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip)
提取此值的最简单方法是什么?或者,我必须手动构建for值吗

向您致意如下:

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> self, 
    Expression<Func<TModel, TValue>> expression, 
    Boolean showToolTip
)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
    var id = self.ViewData.TemplateInfo.GetFullHtmlFieldId(metadata.PropertyName);
    // do something with the id
    ...
}

这篇文章解决了我的问题:

详细内容:

string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)

这只会得到最后一部分,例如标题而不是ModelViewAd_标题?您使用的是EditorTemplates吗?我使用的是html帮助程序,如html.TextBoxFor,但不是html.EditorFor