C # Generics, what am I doing wrong?

I am kind of new to Generics, and I cannot understand why the following does not work.

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

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

Let's say I have a variable in my razor model called Products, and this is a List<Product> , so I tried to do

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

It says: β€œIt is not possible to convert a group of Grid methods into an object without a delegate. Did you intend to call the method?”, With an underlined red underline β€œ@ Model.tasks.Grid”.

The funny thing is that the visual studio is compiling, everything is in order. Of course, if I just do

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

Things are good.

+6
source share
1 answer

The Razor parser interprets <and> as regular HTML tags. You can avoid the problem enclosing the call in parentheses:

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

You can find more information here: How to use the general syntax inside a Razor view file?

+7
source

Source: https://habr.com/ru/post/950682/


All Articles