Asp.net mvc Asp.net Mvc2 LabelFor和prop中的DisplayName
上下文=>Asp.net mvc Asp.net Mvc2 LabelFor和prop中的DisplayName,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,上下文=> ${Html.CheckBoxFor(x => x.Foos[i].Checked)} ${Html.LabelFor(x => x.Foos[i].Checked)} 问题是-我无法提供基于Foo.Name的标签文本 有没有现成的方法来修改元数据,传入lambda x=>x.Name来更改它 正在创建另一个HmtlHelper扩展方法 LabelFor(this htmlhelper, [lambda], value, htmlattribs) 唯一的办法 呃,随
${Html.CheckBoxFor(x => x.Foos[i].Checked)}
${Html.LabelFor(x => x.Foos[i].Checked)}
问题是-我无法提供基于Foo.Name的标签文本
有没有现成的方法来修改元数据,传入lambda x=>x.Name来更改它
正在创建另一个HmtlHelper扩展方法
LabelFor(this htmlhelper, [lambda], value, htmlattribs)
唯一的办法
呃,随便吧。变通办法是可以接受的。这个问题不是showsupper=>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression,
string labelText, object htmlAttributes) where TModel : class
{
TagBuilder builder = new TagBuilder("label");
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); // to convert an object into an IDictionary
string value = ExpressionHelper.GetExpressionText(expression); ;
builder.InnerHtml = labelText;
//the replaces shouldnt be necessary in the next statement, but there is a bug in the MVC framework that makes them necessary.
builder.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(value).Replace('[', '_').Replace(']', '_'));
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
public static MvcHtmlString LabelFor(此HtmlHelper html、表达式、,
字符串labelText,对象htmlAttributes),其中TModel:class
{
标记生成器=新标记生成器(“标签”);
MergeAttributes(新RouteValueDictionary(htmlAttributes));//将对象转换为IDictionary
字符串值=ExpressionHelper.GetExpressionText(表达式);
builder.InnerHtml=labelText;
//在下一个语句中不应该需要替换,但是MVC框架中有一个bug使得它们成为必需。
builder.Attributes.Add(“for”、html.ViewContext.ViewData.TemplateInfo.getFullHtmlFieldDid(value).Replace('[','''.''''.')。Replace(']',''.''''.'));
返回MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}