C # html-helper estender o método existente sem substituir?
Eu já procurei a pergunta e achei possívelresponda, mas ainda preciso de ajuda.
Estou tentando escrever um html-helper para estender a funcionalidade do método LabelFor já existente
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
//string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
var labelText = html.LabelFor(expression);
if (String.IsNullOrEmpty(labelText.ToString()))
{
return MvcHtmlString.Empty;
}
if (metadata.IsRequired)
{
labelText = new MvcHtmlString(labelText.ToString().Substring(0, labelText.ToString().Length - 8).Trim() +
"<span style=\"color:red\" class=\"required-marker\">*</span></label>");
}
TagBuilder tag = new TagBuilder("label");
tag.MergeAttributes(htmlAttributes);
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
tag.SetInnerText(labelText.ToString());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
Estou tentando adicionar uma funcionalidade, em que o método verificaria se a variável tem um sinalizador "obrigatório" e, em seguida, executa alguma coisa (nesse caso, adiciona vermelho * no final do rótulo)
[Required]
[Display(Name = "Year")]
public string ProjectYr { get; set; }
No entanto, sinto que estou substituindo toda a funcionalidade LabelFor. Existe uma maneira de simplesmente adicionar nova funcionalidade ao método LabelFor existente, preservando todas as funções do original sem substituí-lo?Sobrepor não funciona de qualquer maneira, já que meu método é estático.
Muito obrigado antecipadamente!