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>