asp.net-mvc – Asp.Net MVC 2 Label自定义文本
发布时间:2020-12-16 00:40:13 所属栏目:asp.Net 来源:网络整理
导读:有没有办法使用LabelFor帮助器并自定义标签文本,而不必在我的模型中使用DisplayNameAttribute? 解决方法 我为我的项目创建了这个html助手: public static class MyLabelExtensions{ public static MvcHtmlString Label(this HtmlHelper htmlHelper,string
有没有办法使用LabelFor帮助器并自定义标签文本,而不必在我的模型中使用DisplayNameAttribute?
解决方法
我为我的项目创建了这个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) %> 希望有帮助… (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 我可以在MVC 3中有/多个_Layout页面吗?为c
- asp.net-mvc-4 – 如何使用Hot Towel角色使用Windows身份验
- asp.net-mvc – 在MVC3中使用Workflow Foundation来处理页面
- asp.net – 改进MonoRail
- asp.net-mvc – 如何在asp.net中使用mvc构建购物车?
- ASP.NET MVC从请求参数中删除双正斜杠
- 是否有任何ASP.NET身份的实现具有另一个级别以上的帐户?
- .net – 从Orchard内的主题渲染图像
- 手动搭建自己的nuget服务器及使用
- ASP.NET是多线程的(它如何执行请求)
推荐文章
站长推荐
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net-mvc – 动态URL路由ASP MVC
- 什么是asp.net mvc网站的正常内存使用情况?
- asp.net-mvc-4 – MVC4 Kendo项目Ajax.BeginForm
- 你使用ASP.NET MVC Action方法做了什么聪明的事情
- asp.net – Azure AD B2C – 多个子域
- asp.net – MvcHtmlString.ToHtmlString()不编码
- asp.net-mvc – ASP.NET MVC Scheduler开源?
- asp.net – 如何在Visual Studio 2008调试器中查
- 运行SonarQube针对ASP.Net核心解决方案/项目
热点阅读