7

I'm some what new to Generics and I can't figure out why the following doesn't work.

I have an extension to IEnumerable<T>, called Grid and it looks like so

  public static class IEnumberableGridExtension
  {
        public static HelperResult Grid<T>(this IEnumerable<T> gridItems, Action<GridView<T>> thegrid) 
        {
            ........
        }

   }

Say, I have a variable in my razor Model called "Products" and it is the type List<Product>, so I tried to do

@Model.Products.Grid<Product>(grid=>{
...
});

It states "Cannot convert method group 'Grid" to non-delegate type 'object'. Did you intend to invoke the method?", with "@Model.tasks.Grid" red underlined.

The funny thing is, visual studio compiles, everything is fine. Of course, if i simply do

@Model.Products.Grid(grid=>{
...
});

It's all fine.

Liming
  • 1,641
  • 3
  • 28
  • 38
  • You stated that it's a list of generic type `Products`. Was that a typo? If not, you need to change the first line to `@Model.Products.Grid(...` – John Jul 31 '13 at 03:34
  • Sorry John. It's singular 'Product'. I'll fix it in the post – Liming Jul 31 '13 at 03:36
  • What happens if you pass in null for the Action? – Justin Pihony Jul 31 '13 at 03:38
  • @Liming The razor editor can be quite finicky at times. Have you tried simply closing visual studio and restarting? – p.s.w.g Jul 31 '13 at 03:38
  • Maybe you need to specify the dll reference to `IEnumberableGridExtension` in web.config under views folder – zs2020 Jul 31 '13 at 03:39
  • From the code you've provided, that error should be nonexistent. Might wanna run a Clean – John Jul 31 '13 at 03:39
  • Seems not possible. http://stackoverflow.com/questions/4760783/is-it-possible-to-create-a-generic-helper-method-with-razor – Prash Jul 31 '13 at 03:39
  • John, did clean and rebuild, same. Justin, setting to null will spit out an empty grid. My code handles that scenario. P.S.W.G thanks for your help once again (btw, i decided to use generic method for my grid :) and I restarted VS 2010, same thing. Compiles fine, but in the editor gives me that error. Sza, i did put the namespace under web.config in /views, no dice. – Liming Jul 31 '13 at 03:43
  • There are other ways in which you can do this. Listed here.. http://stackoverflow.com/questions/4965680/razor-support-of-generic-extension-methods – Prash Jul 31 '13 at 03:45
  • 1
    @Prash, what's wrong with creating an extension method? It's not a razor helper extension, it's just a plain old IEnumerable – John Jul 31 '13 at 03:47
  • @John: Agree with you. However, anything between angular (<>) be interpreted as html tags rather than generic types. Unfortunately, same Angular braces would be used for Extensions and HTML. So no wonder that because of this ambiguous nature it is not being supported. – Prash Jul 31 '13 at 03:52

1 Answers1

8

The Razor parser is interpreting < and > as normal HTML tags. You can avoid the problem wrapping the call in parenthesis:

@(Model.Products.Grid<Product>(grid=>{
...
}))

You can find additional info here: How to use generic syntax inside a Razor view file?

Community
  • 1
  • 1
fcuesta
  • 4,429
  • 1
  • 18
  • 13
  • BINGO! Thank you so much @fcuesta!!!! Been searching around for 2 hours already, my goodness! Microsoft really ought to get it fixed, can't believe it's something this simple! – Liming Jul 31 '13 at 04:01