Wydłuż brzytwę MVC3 Html.LabelDo dodania klasy css
Próbuję dodać klasę css do Html.LabelFor na EditorTemplate
@Html.LabelFor(model => model.Name, new { @class = "myLabel" })
moje oczekiwania na przykład: etykieta powinna odebrać klasę css.
W tym celu próbowałem rozszerzyć etykietę za pomocą następującego kodu, ale moja etykieta nie jest wyświetlana. Co ja tu robię źle?
public static class LabelExtensions
{
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return html.LabelFor(expression, null, htmlAttributes);
}
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
{
return html.LabelHelper(
ModelMetadata.FromLambdaExpression(expression, html.ViewData),
ExpressionHelper.GetExpressionText(expression),
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
labelText);
}
private static MvcHtmlString LabelHelper(this HtmlHelper html, ModelMetadata metadata, string htmlFieldName, IDictionary<string, object> htmlAttributes, string labelText = null)
{
var str = labelText
?? (metadata.DisplayName
?? (metadata.PropertyName
?? htmlFieldName.Split(new[] { '.' }).Last()));
if (string.IsNullOrEmpty(str))
return MvcHtmlString.Empty;
var tagBuilder = new TagBuilder("label");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
tagBuilder.SetInnerText(str);
return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
}
private static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode)
{
return new MvcHtmlString(tagBuilder.ToString(renderMode));
}
}
Jeśli nie podam na przykład klasy css@Html.LabelFor(model => model.Name)
, to pokazuje etykietę. Ale nie jestem w stanie zastosować css do mojej etykiety. Chcę pokazać etykietę w kolorze niebieskim, gdy strona się ładuje, a następnie zmienić styl etykiety na podstawie akcji użytkownika za pomocą jquey.All jest w porządku, na mojej stronie z wyjątkiem faktu, że nie mogę rozszerzyć i dodać klasy css do mojej etykiety.