Html inside label using Html helper

后端 未结 5 1236
甜味超标
甜味超标 2020-12-03 11:14

How can I add inline html elements inside a label with Html.Label?

5条回答
  •  独厮守ぢ
    2020-12-03 12:14

    I borrowed upon Darin's answer, and added to it. I added in capability for Html before the label text and html after the label text. I also added a bunch of overload methods and comments.

    I also got some information from this post: How can I override the @Html.LabelFor template?

    Hope if helps folks.

    namespace System.Web.Mvc.Html
    {
        public static class LabelExtensions
        {
            /// Creates a Label with custom Html before the label text.  Only starting Html is provided.
            /// Html to preempt the label text.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml)
            {
                return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}"));
            }
    
            /// Creates a Label with custom Html before the label text.  Starting Html and a single Html attribute is provided.
            /// Html to preempt the label text.
            /// A single Html attribute to include.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, object htmlAttributes)
            {
                return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAttributes));
            }
    
            /// Creates a Label with custom Html before the label text.  Starting Html and a collection of Html attributes are provided.
            /// Html to preempt the label text.
            /// A collection of Html attributes to include.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, IDictionary htmlAttributes)
            {
                return LabelFor(html, expression, startHtml, null, htmlAttributes);
            }
    
            /// Creates a Label with custom Html before and after the label text.  Starting Html and ending Html are provided.
            /// Html to preempt the label text.
            /// Html to follow the label text.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml)
            {
                return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}"));
            }
    
            /// Creates a Label with custom Html before and after the label text.  Starting Html, ending Html, and a single Html attribute are provided.
            /// Html to preempt the label text.
            /// Html to follow the label text.
            /// A single Html attribute to include.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, object htmlAttributes)
            {
                return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAttributes));
            }
    
            /// Creates a Label with custom Html before and after the label text.  Starting Html, ending Html, and a collection of Html attributes are provided.
            /// Html to preempt the label text.
            /// Html to follow the label text.
            /// A collection of Html attributes to include.
            /// MVC Html for the Label
            public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, IDictionary htmlAttributes)
            {
                ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
                string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
    
                //Use the DisplayName or PropertyName for the metadata if available.  Otherwise default to the htmlFieldName provided by the user.
                string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
                if (String.IsNullOrEmpty(labelText))
                {
                    return MvcHtmlString.Empty;
                }
    
                //Create the new label.
                TagBuilder tag = new TagBuilder("label");
    
                //Add the specified Html attributes
                tag.MergeAttributes(htmlAttributes);
    
                //Specify what property the label is tied to.
                tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
    
                //Run through the various iterations of null starting or ending Html text.
                if (startHtml == null && endHtml == null) tag.InnerHtml = labelText;
                else if (startHtml != null && endHtml == null) tag.InnerHtml = string.Format("{0}{1}", startHtml(null).ToHtmlString(), labelText);
                else if (startHtml == null && endHtml != null) tag.InnerHtml = string.Format("{0}{1}", labelText, endHtml(null).ToHtmlString());
                else tag.InnerHtml = string.Format("{0}{1}{2}", startHtml(null).ToHtmlString(), labelText, endHtml(null).ToHtmlString());
    
                return MvcHtmlString.Create(tag.ToString());
            }
        }
    }
    

提交回复
热议问题