OK, I’m trying to implement the Repeater extension methods to the HtmlHelper as explained in Phil Haack’s blog here http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
However, when I try to use it in my View I get a Compilation error ‘System.Web.Mvc.HtmlHelper’ does not contain a definition for ‘Repeater’.
Here is my extension class:
namespace MyAwesomeBlog.Helpers {
public static class HtmlHelpers {
public static void Repeater<T>(this HtmlHelper html
, IEnumerable<T> items
, Action<T> render
, Action<T> renderAlt) {
// Implementation irrelevant
});
}
public static void Repeater<T>(this HtmlHelper html
, Action<T> render
, Action<T> renderAlt) {
// Implementation irrelevant
});
}
public static void Repeater<T>(this HtmlHelper html
, string viewDataKey
, Action<T> render
, Action<T> renderAlt) {
// Implementation irrelevant
});
}
public static void Repeater<T>(this HtmlHelper html
, IEnumerable<T> items
, string className
, string classNameAlt
, Action<T, string> render) {
// Implementation irrelevant
});
}
}
}
I have included this in my Web.Config:
<add namespace="MyAwesomeBlog.Helpers"/>
This is my use of the extension method in my view:
<% HtmlHelper.Repeater<Post>(Model, "post", "post-alt", (post, cssClassName) => { %>
<div class="<%=cssClassName %>">
<h1><%= post.Title %></h1>
<p>
<%= post.Body %>
</p>
</div>
<% }); %>
Still, the compiler gives me squiggly lines under “.Repeater” saying that HtmlHelper does not have such a method.
What have I missed?
Regarding my comment in my other answer, I have just checked and I am almost sure that is your problem. You can’t have extension methods on static classes (or add static extension methods), so you need an instance of HtmlHelper to call Repeater.