I can't understand how this piece of code works for the life of me, please help!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 Jahre weitere
This is from CategoryTemplate.ProductsInGridOrLines.cshtml


@(Html.DataList<ProductModel>(Model.FeaturedProducts, 2,
                    @<div class="item-box">
                        @Html.Partial("_ProductBox", @item)
                    </div>
                ))


Now I get the general idea. I also have an adequate understanding of how generics and delegates work to grasp the role they play in producing the resulting HTMLString.

But, but but....

I can't figure out how the second parameter to Html.Partial is instantiated the way it is. It is the object represented by "@item", which is of type ProductModel, and which is the ProductModel that is being iterated over at the time the DataList function goes through its first parameter, itself a collection of ProductModels.

For all intents and purposes, the type of @item should not be recognized by the IDE at coding time, but it does! Intellisense is happy to provide me with the type of @item!

I understand what it is and what it does, but I am asking how this is possible. In other words, I am looking for an answer that explains the inner workings of the part of  MVC and/or Nop framework that makes this happen.

If you take a look at rest of the view, you'll notice that @item is not defined in it. There is no outer loop that instantiates an @item for you. And yet there it is, all properly instantiated and populated, exposing its data to the _ProductBox.cshtml.

I'm new to MVC, and I've already done a fair bit googling over this problem, but to no avail! Please help me solve this annoying puzzle!
11 Jahre weitere
Have a look in Nop.Web.Framework.UI

There's an extension class in there called DataListExtensions, which has the following signature

      public static IHtmlString DataList<T>(this HtmlHelper helper, IEnumerable<T> items, int columns,
            Func<T, HelperResult> template)

This is what the UI is calling from this line

@(Html.DataList<ProductModel>(Model.FeaturedProducts, 2,
                    @<div class="item-box">
                        @Html.Partial("_ProductBox", @item)
                    </div>
                ))

So, the UI is telling the function the generic type (Html.DataList<ProductModel>), and the Func is using that type - that's how the IDE knows what @item is!

Have a look at the class, it'll all become clear :)
11 Jahre weitere
Well, thanks for the answer Dylan, but that much I had already figured out.

Anyways, I've just happened upon this (http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx) from Haacked - it explains the magic behind @item.
11 Jahre weitere
Merhaba Ahmet BEY
Biz inveon kendi yazılımını geliştiriyor sanıyorduk.
Sanırım siz de bedava yazılımı alıp firmalara yüksek fiyata satan bir kurumsunuz. Toplantılarda bunu bellirtmediniz ama bunu firmamız ile paylaşacağım.
Oldukça ücüzü bir durum açıkçası
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.