ASP.NET MVC 2 Preview 2 - Extension LabelExtensions.LabelFor

I am wondering if anyone tried to write an extension helper for LabelExtensions.LabelFor HtmlHelper in MVC2? This would be useful for me in that my application requires me to always write tags in a <td> tag with a class attribute. Instead of repeating this code in a view, I thought I could write a small extension method:

public static MvcHtmlString RenderLabelFor<TModel, TValue> ( this HtmlHelper html, Expression<Func<TModel, TValue>> value, object htmlAttributes ) where TModel : class { TagBuilder builder = new TagBuilder("td"); builder.MergeAttributes(new RouteValueDictionary(attributes)); // to convert an object into an IDictionary builder.InnerHtml = LabelExtensions.LabelFor(html, value).ToString(); return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal)); } 

However, I get an error on the LabelFor line:

Type arguments to the method "System.Web.Mvc.Html.LabelExtensions.LabelFor (System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)" cannot be taken out of use. Try explicitly specifying type arguments.

Can anyone throw me a bone on this?

+4
source share
2 answers

It may be too late to help you, but you need to use a generic version of HtmlHelper in your method signature, for example:

 public static MvcHtmlString RenderLabelFor<TModel, TValue> ( this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> value, object htmlAttributes ) { ... } 
+3
source

try

 public static MvcHtmlString RenderLabelFor<TModel, TValue> ( HtmlHelper html, <Func<TModel, Value>> value, object htmlAttributes) where TModel : class 
0
source

All Articles