Html.Label
ve Html.LabelFor
yardımcı yöntemler, diğer yardımcıların çoğu gibi htmlAttributes parametresini desteklemez. Ancak class
'u ayarlamak istiyorum. Böyle bir şey:MVC3 Label ve LabelFor HTML yardımcıları nasıl genişletilir?
Html.LabelFor(model => model.Name, new { @class = "control-label" })
kopyalama başvurmadan ve bu yöntemlerin ILSpy disasm çıkışını uzanan olmadan Etiket/LabelFor uzanan bir kolay yolu var mı? ,
böyleBir şey
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
}
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();
if (String.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("label");
tag.MergeAttributes(htmlAttributes);
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
tag.SetInnerText(labelText);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
Güncelleme sadece proje oluşturulan uzatma yöntemi kullanmak için gerekenleri yapmalıdır:
Biliyorum ama ben sökülüp/orijinal kaynak çıkışını kopyalayan önlemek çalışıyorum. Orijinal kod 22 yöntemden oluşuyor, bunu doğru bir şekilde yapmak için hepsini geçersiz kılmalıyım. – batkuip
no. sadece uzantı projenizi oluşturup projenize çağrılarınızı web.config dosyanıza eklersiniz. – Iridio