Reputation: 5976
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?
Upvotes: 1
Views: 4831
Reputation: 19
Extend the IHtmlHelper
interface rather than HtmlHelper
class.
public static void Repeater<T>(this IHtmlHelper html
, IEnumerable<T> items
, string className
, string classNameAlt
, Action<T, string> render) {
Upvotes: 1
Reputation: 33071
Try changing it to:
<% Html.Repeater<Post>(Model, "post", "post-alt", (post, cssClassName) => { %>
<div class="<%=cssClassName %>">
<h1><%= post.Title %></h1>
<p>
<%= post.Body %>
</p>
</div>
<% }); %>
The Html property on your View is an HtmlHelper.
Upvotes: 1
Reputation: 1192
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.
Upvotes: 3
Reputation: 1192
I came across this very problem today and in my case closing and reopening the page with the html on it seemed to do the trick (and compiling the project obviously).
Upvotes: 0
Reputation: 9431
Did you add this to the Web.Config in the Views folder or the root web.config? It needs to go in 'Views/web.config'.
Upvotes: 3