【问题标题】:Can I add a CSS class definition to the Html.LabelFor in MVC3?我可以在 MVC3 中的 Html.LabelFor 中添加 CSS 类定义吗?
【发布时间】:2013-07-24 01:29:26
【问题描述】:

我希望有人有一些想法。我想整理我的代码,所以我已经使用了 Html.LabelFor。但是现在我想为标签分配一个 CSS 类。

Html.LabelFor(model => model.Customer.Description   ????)

有没有人知道这在 MVC3 中是否可行。请注意,我使用的是 MVC3。我已经看到了一篇关于 MVC2 的帖子,并且没有简单的解决方案。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    给你,伙计-o:

    namespace System.Web.Mvc.Html
    {
      using System;
      using Collections.Generic;
      using Linq;
      using Linq.Expressions;
      using Mvc;
    
      public static class LabelExtensions
      {
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
          return html.LabelFor(expression, null, htmlAttributes);
        }
    
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
        {
          return html.LabelHelper(
                ModelMetadata.FromLambdaExpression(expression, html.ViewData),
                ExpressionHelper.GetExpressionText(expression),
                HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
                labelText);
        }
    
        private static MvcHtmlString LabelHelper(this HtmlHelper html, ModelMetadata metadata, string htmlFieldName, IDictionary<string, object> htmlAttributes, string labelText = null)
        {
          var str = labelText
                ?? (metadata.DisplayName
                ?? (metadata.PropertyName
                ?? htmlFieldName.Split(new[] { '.' }).Last()));
    
          if (string.IsNullOrEmpty(str))
            return MvcHtmlString.Empty;
    
          var tagBuilder = new TagBuilder("label");
          tagBuilder.MergeAttributes(htmlAttributes);
          tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
          tagBuilder.SetInnerText(str);
    
          return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
        }
    
        private static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode)
        {
          return new MvcHtmlString(tagBuilder.ToString(renderMode));
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      在 MVC 3 中没有内置的方法来执行此操作。您必须编写执行此操作的帮助程序。看看LabelExtensions 类,看看它是如何完成的。

      【讨论】:

      • 谢谢。只是想确认一下 MVC3 中没有什么新东西。我想知道他们为什么不给 Html.LabelFor 更多的灵活性。我想我不是唯一一个想要这样做的人。
      • @LeeGarner 好吧,我在 MVC 团队中,我不确定为什么 Label 助手不采用 htmlAttributes 参数。也许有些疏忽。我会提交一个错误以确保我们在下一个版本中添加它。
      • @marcind 当您使用它时,您可以取消内部帮助程序,即。在 LabelExtensions 中,LabelHelper 是内部的,所以我们在滚动自己的辅助扩展时不能使用它。
      • @George R - 下一个版本的 MVC 将支持 htmlAttributes 参数,就像所有其他帮助程序一样,以启用此方案。不过,我们可能不会公开 LabelHelper 函数。但我希望这能满足您的需求。
      • 为了方便大家阅读,MVC4 允许使用 htmlAttributes 参数,解决了这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      相关资源
      最近更新 更多