HTML Tag in Helper Razor

1

I have HTML code:

<label class="labelinput">Nº Compra<em>*</em></label>

Moving on to Helper ran as follows:

@Html.Label("Nº Compra:", new {@class = "labelinput"})

But I could not add the <em> tag to Helper.

How do I add the <em> tag to get the helper string together?

    
asked by anonymous 11.09.2015 / 15:35

2 answers

0

Dude, you just do it like this:

<em>
  @Html.Label("Nº Compra:", new {@class = "labelinput"})
</em>
    
28.12.2015 / 18:11
0

According to this answer from Darin Dmitrov , implement the following extension (modified to reflect your scenario):

namespace MeuProjeto.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString Label(this HtmlHelper<TModel> htmlHelper, String ex, Func<object, HelperResult> template, object htmlAttributes)
        {
            var label = new TagBuilder("label");
            label.MergeAttributes(htmlAttributes);
            label.InnerHtml = string.Format(
                "{0} {1}", 
                ex,
                template(null).ToHtmlString()
            );
            return MvcHtmlString.Create(label.ToString());
        }
    }
}

Usage:

@Html.Label("Nº Compra",
    @"<em>*</em>",
    new { @class = "labelinput" }
)

A tip: register the namespace of your extensions in web.config that is inside the Views directory:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        ...
        <add namespace="MeuProjeto.Extensions" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
    
28.12.2015 / 19:08