asp.net-mvc – 具有属性列表的ASP.Net MVC Html.Label?
发布时间:2020-12-16 03:40:57 所属栏目:asp.Net 来源:网络整理
导读:如何为 Html.Label创建一个ASP.Net MVC Helper,它接受属性? 目前,当我定义一个Html.TextBox时,我能够传入一个属性列表.有点像下面这样: new {disabled="disabled",@class="pcTextBoxWithoutPaddingDisabled"})% 但是,它看起来并不像Html.Label具有此功能.
如何为
Html.Label创建一个ASP.Net MVC Helper,它接受属性?
目前,当我定义一个Html.TextBox时,我能够传入一个属性列表.有点像下面这样: new {disabled="disabled",@class="pcTextBoxWithoutPaddingDisabled"})%> 但是,它看起来并不像Html.Label具有此功能.因此,我必须使用label标签定义我的标签.有点像下面这样: <label class="pcLabelBlackWithoutPadding"> 我希望能够保持我的Html元素的创建方式. 那么,我如何创建一个Html.Label,它将包含一个属性列表? 谢谢你的帮助. 解决方法
这是MVC3的更新版本:
public static MvcHtmlString Label(this HtmlHelper helper,String htmlFieldName,String labelText,Object htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromStringExpression(htmlFieldName,helper.ViewData); String innerText = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split('.').Last())); if (String.IsNullOrEmpty(innerText)) { return MvcHtmlString.Empty; } TagBuilder tagBuilder = new TagBuilder("label"); tagBuilder.Attributes.Add("for",TagBuilder.CreateSanitizedId(helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); tagBuilder.SetInnerText(innerText); return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何从MVC asp.net控制器为javascript变量赋值
- asp.net-mvc – 加载符号时Visual Studio调试痛苦地慢
- asp.net-mvc – ASP.NET MVC:获取RouteTable路由的URL
- asp.net-mvc – 如何在MVC控制器中使用消息框?
- asp.net-mvc-3 – 带有附加HTML的MVC3 Html.BeginForm()
- ASP.NET身份验证登录和退出浏览器返回按钮
- 在ASP.Net Cookie或会话状态中存储会话信息?
- 为什么在使用当前同步上下文启动任务时,不设置ASP.NET Http
- 将数据从asp.net-mvc传递到javascript的最佳做法
- asp.net – Telerik RadAjaxManager仍然回发