解决方法
我为我的项目创建了这个html助手:
public static class MyLabelExtensions { public static MvcHtmlString Label(this HtmlHelper htmlHelper,string forName,string labelText) { return Label(htmlHelper,forName,labelText,(object) null); } public static MvcHtmlString Label(this HtmlHelper htmlHelper,string labelText,object htmlAttributes) { return Label(htmlHelper,new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString Label(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes) { var tagBuilder = new TagBuilder("label"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("for",forName.Replace(".",tagBuilder.IdAttributeDotReplacement),true); tagBuilder.SetInnerText(labelText); return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)); } public static MvcHtmlString LabelFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,string labelText) { return LabelFor(htmlHelper,expression,(object) null); } public static MvcHtmlString LabelFor<TModel,object htmlAttributes) { return LabelFor(htmlHelper,new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString LabelFor<TModel,object> htmlAttributes) { string inputName = ExpressionHelper.GetExpressionText(expression); return htmlHelper.Label(inputName,htmlAttributes); } }
我用“强类型”资源使用它们:
<%= Html.LabelFor(m=>m.NickName,UserStrings.NickName) %>
希望有帮助…