1

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?

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had just figured it out and was coming back to answer my own question when I saw that this answer of yours was exactly right...it turns out I had to be calling Html.Repeater, and not HtmlHelper.Repeater.
3

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'.

4 Comments

ok it was indeed in the root web.config. I moved it to the views/web.config, but sadly this was to no avail. I still get the compilation error at runtime.
Do you actually get YSOD or is just intellisense complaining? Did you compile the project?
The solution compiles and launches. Then I get the YSOD, pointing at the "<% HtmlHelper.Repeater<Post>(Model, "post", "post-alt", (post, cssClassName) =>" line of my View's code, saying that HtmlHelper does not contain a definition for Repeater.
Are you sure the .cs file which has the extension methods has been added to the project? Add a syntax error and compile. If it is part of the project compilation will fail.
1

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.

Comments

1

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) {

Comments

0

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).

2 Comments

Nope...I've even closed and re-opened Visual Studio...also to no avail.
I am more familiar with the razor syntax, but with razor there is an instance of the HtmlHelp object (@Html) which has the custom helpers (once you add the config etc.). You are calling HtmlHelper.Repeater, suggesting it is a static method on the HtmlHelper object....are you sure you have an instance of an HtmlHelper?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.