2011-04-18 12 views
9

का उपयोग कर लेबल के अंदर एचटीएमएल मैं HTML.Label के साथ लेबल के अंदर इनलाइन HTML तत्व कैसे जोड़ सकता हूं?एचटीएमएल हेल्पर

उत्तर

19

एक कस्टम सहायक के लिए एक अच्छा परिदृश्य की तरह लग रहा

@Html.LabelFor(
    x => x.Name, 
    @<span>Hello World</span> 
) 

अद्यतन:

प्राप्त करने के लिए क्या आप टिप्पणी अनुभाग में पूछा आप निम्न का प्रयास कर सकते हैं:

public static class HtmlHelperExtensions 
{ 
    public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template) 
    { 
     var htmlFieldName = ExpressionHelper.GetExpressionText(ex); 
     var propertyName = htmlFieldName.Split('.').Last(); 
     var label = new TagBuilder("label"); 
     label.Attributes["for"] = TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)); 
     label.InnerHtml = string.Format(
      "{0} {1}", 
      propertyName, 
      template(null).ToHtmlString() 
     ); 
     return MvcHtmlString.Create(label.ToString()); 
    } 
} 

और उसके बाद:

@Html.LabelFor(
    x => x.Name, 
    @<em>mandatory</em> 
) 
+1

@ डैरिन-दिमित्रोव आह शांत, मैं इसे कैसे का नाम लिखने के लिए प्राप्त कर सकते हैं संपत्ति? मैं परिणाम Marcus

+0

@Marcus, कृपया मेरा अपडेट देखें। –

+0

@ डारिन-डिमिट्रोव, मदद करने की कोशिश करने के लिए बहुत बहुत धन्यवाद लेकिन मुझे लगता है कि मैं थोड़ा अस्पष्ट था। जिसे मैं प्रॉपर्टीनाम नामक लेबल वैल्यू को अपनी संपत्ति पर डिस्प्ले एट्रिब्यूट या सामान्य लेबल कामों के नाम पर निर्दिष्ट मान होना चाहिए। – Marcus

2

आपको अपना खुद का सहायक लिखना होगा। अंतर्निहित Html.Label सहायक स्वचालित रूप से HTML-encodes labelText पैरामीटर।

public static class LabelExtensions 
{ 
    public static MvcHtmlString LabelFor<TModel, TProperty>(
     this HtmlHelper<TModel> htmlHelper, 
     Expression<Func<TModel, TProperty>> ex, 
     Func<object, HelperResult> template 
    ) 
    { 
     var htmlFieldName = ExpressionHelper.GetExpressionText(ex); 
     var for = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); 
     var label = new TagBuilder("label"); 
     label.Attributes["for"] = TagBuilder.CreateSanitizedId(for); 
     label.InnerHtml = template(null).ToHtmlString(); 
     return MvcHtmlString.Create(label.ToString()); 
    } 
} 

और उसके बाद:

2

मैं डैरिन के जवाब पर उधार लिया है, और यह करने के लिए जोड़ा। मैंने लेबल टेक्स्ट के बाद लेबल टेक्स्ट और एचटीएमएल से पहले एचटीएमएल की क्षमता में जोड़ा। मैंने अधिभार विधियों और टिप्पणियों का एक गुच्छा भी जोड़ा। How can I override the @Html.LabelFor template?

आशा लोगों में मदद करता है, तो:

मैं भी इस पोस्ट से कुछ जानकारी मिल गया।

@Html.LabelFor(m => m.Phone,true) 

करने के लिए:

public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex,bool applyStylingHtml) 
    { 
     var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData); 
     string displayName = metadata.DisplayName; 
     string description= metadata.Description; 
     if (String.IsNullOrEmpty(displayName)) 
     { 
      return MvcHtmlString.Empty; 
     } 

     var sb = new StringBuilder(); 
     sb.Append(displayName); 


     var htmlFieldName = ExpressionHelper.GetExpressionText(ex); 
     var propertyName = htmlFieldName.Split('.').Last(); 

     var tag = new TagBuilder("label"); 
     tag.Attributes["for"] = TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)); 
     tag.SetInnerText(sb.ToString()); 

     //Func<object, HelperResult> template='<em>'; 
     HtmlString nestedHtml=new HtmlString("<em>"+description+"</em>"); 
     tag.InnerHtml = string.Format(
      "{0} {1}", 
      tag.InnerHtml, 
      nestedHtml 
     ); 

     return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); 
    } 

फिर उस्तरा कोड में इसका इस्तेमाल करते हैं:

namespace System.Web.Mvc.Html 
{ 
    public static class LabelExtensions 
    { 
     /// <summary>Creates a Label with custom Html before the label text. Only starting Html is provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml) 
     { 
      return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}")); 
     } 

     /// <summary>Creates a Label with custom Html before the label text. Starting Html and a single Html attribute is provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <param name="htmlAttributes">A single Html attribute to include.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, object htmlAttributes) 
     { 
      return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAttributes)); 
     } 

     /// <summary>Creates a Label with custom Html before the label text. Starting Html and a collection of Html attributes are provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <param name="htmlAttributes">A collection of Html attributes to include.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, IDictionary<string, object> htmlAttributes) 
     { 
      return LabelFor(html, expression, startHtml, null, htmlAttributes); 
     } 

     /// <summary>Creates a Label with custom Html before and after the label text. Starting Html and ending Html are provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <param name="endHtml">Html to follow the label text.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml) 
     { 
      return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}")); 
     } 

     /// <summary>Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a single Html attribute are provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <param name="endHtml">Html to follow the label text.</param> 
     /// <param name="htmlAttributes">A single Html attribute to include.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, object htmlAttributes) 
     { 
      return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAttributes)); 
     } 

     /// <summary>Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a collection of Html attributes are provided.</summary> 
     /// <param name="startHtml">Html to preempt the label text.</param> 
     /// <param name="endHtml">Html to follow the label text.</param> 
     /// <param name="htmlAttributes">A collection of Html attributes to include.</param> 
     /// <returns>MVC Html for the Label</returns> 
     public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, IDictionary<string, object> 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()); 
     } 
    } 
} 
+0

मैं इसका उपयोग कैसे करूं? – Terkhos

+0

मैंने उपरोक्त टेक्स्ट को लेबलफॉर.c नामक फ़ाइल में गिरा दिया जिसे मैंने "हेल्पर्स" निर्देशिका में रखा था। इसका उपयोग @ Html.LabelFor() विधि का उपयोग कर मेरे किसी भी रेजर .cshtml में किया जाता है। – Setarcos

1

आदेश एसओसी और ठोस सिद्धांतों को पूरा करने के कोड निम्न कोड को बढ़ाया जा सकता है सबकुछ अधिक गतिशील बनाएं, मॉडल श्रेणी पर वर्णन विशेषता लागू की जानी चाहिए, फिर HtmlHelper विवरण को "em" एचटीएमएल टैग लागू करने के लिए एक पाठ के रूप में ले जाएगा:

[Display(Name ="Phone",Description = "should be included extention")] 
public string Phone { get; set; } 

बस एक सिर ऊपर है कि आप को जोड़कर देखने के लिए अपने अनुकूलित HtmlHelper नाम स्थान आयात करने की आवश्यकता:

@using yourNamespace 
संबंधित मुद्दे