asp.net-mvc – 我能改变MVC中LabelFor渲染的方式吗?
发布时间:2020-12-16 03:57:17 所属栏目:asp.Net 来源:网络整理
导读:我想改变LabelFor渲染的方式.我可以使用DisplayTemplate吗? LabelFor生成标签标签,我想在标签的末尾添加“:”. 谢谢! 亚历克斯 解决方法 这是一个 HTML Helper,它将执行此操作: public static class LabelExtensions { [SuppressMessage("Microsoft.Desi
我想改变LabelFor渲染的方式.我可以使用DisplayTemplate吗?
LabelFor生成标签标签,我想在标签的末尾添加“:”. 谢谢! 亚历克斯 解决方法
这是一个
HTML Helper,它将执行此操作:
public static class LabelExtensions { [SuppressMessage("Microsoft.Design","CA1006:DoNotNestGenericTypesInMemberSignatures",Justification = "This is an appropriate nesting of generic types")] public static MvcHtmlString SmartLabelFor<TModel,TValue>(this HtmlHelper<TModel> html,Expression<Func<TModel,TValue>> expression) { return LabelHelper(html,ModelMetadata.FromLambdaExpression(expression,html.ViewData),ExpressionHelper.GetExpressionText(expression)); } internal static MvcHtmlString LabelHelper(HtmlHelper html,ModelMetadata metadata,string htmlFieldName) { string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(labelText)) { return MvcHtmlString.Empty; } // uncomment if want * for required field //if (metadata.IsRequired) labelText = labelText + " *"; labelText = labelText + ":"; TagBuilder tag = new TagBuilder("label"); tag.Attributes.Add("for",html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); tag.SetInnerText(labelText); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } } 要使用它: <%:Html.SmartLabelFor(m => m.FirstName)%> 它将呈现: <label for="FirstName">First Name:</label> 或者,如果您取消注释所需的字段* <label for="FirstName">First Name *:</label> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何将当前的asp.net usercontrols类名转换为c#上的字符串?
- ASP.NET 4中的可扩展输出缓存(可以缓存页面/控件等)
- EFCore 5 中的 DbContextFactory
- asp.net – 如何以编程方式将参数传递给SSRS报告
- asp.net-mvc – 将Elmah与WebApi一起使用时会记录两次错误
- 从ASP.NET MVC 2中排除授权操作
- asp.net-mvc – 请求验证 – ASP.NET MVC 2
- asp.net – 无论如何要在上传之前确定文件大小?
- asp.net-mvc-2 – 如何使用Castle Windsor在MVC中注入UrlHe
- asp.net-mvc – asp.net mvc:TryUpdateModel返回值还是Mod
推荐文章
站长推荐
- asp-classic – 经典asp中的串联字符串
- asp.net-mvc-2 – Html.Hidden()插入错误的值
- 为什么私有事件处理程序在ASP.NET中不起作用
- asp.net – ASP计数器 – 不同计数器“桶”中类似
- 使用ASP.NET共享托管的十大陷阱
- asp.net – 组合两个List(Of String)最有效的方法
- ASP.Net OData在LB中通过SSL终止失败
- asp.net-core – 使用NLog记录请求正文和请求标头
- asp.net-mvc-3 – 从HttpContext.Current访问Tem
- asp.net-core – ASP.NET Core 1.0 Synchronizat
热点阅读