Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics, what am I doing wrong?

Tags:

c#

razor

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.

like image 670
Liming Avatar asked Jul 31 '13 03:07

Liming


1 Answers

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?

like image 135
fcuesta Avatar answered Nov 03 '22 00:11

fcuesta