I'm using TeaCommerce and as a way to categorize products I've created a field on each Store Category document type that uses the Ucomponents Multi Node Tree Picker to allow the user to select any pages that are of the Product document type. Currently my code is as follows:
@using TeaCommerce.Umbraco.Web
@using TeaCommerce.Api.Models
@using TeaCommerce.Api.Common
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
//Store id
long storeId = long.Parse( Model._Store );
//Product
string ProductTypeName = "Product";
string StoreCategoryName = "StoreCategory";
DynamicNodeList categories = ( Model as DynamicNode ).Descendants( p => p.NodeTypeAlias == StoreCategoryName && p.Level == Model.Level + 1 );
DynamicNodeList products = ( Model as DynamicNode ).Descendants( p => p.NodeTypeAlias == ProductTypeName );
However, I'm getting an error on the line of the nested foreach statement that says "'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'productsPicker' and no extension method 'productsPicker' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)" productsPicker is the alias of the Multi Node Tree Picker field that is on my category document type.
Multi Node Tree Picker in Nested Foreach
I'm using TeaCommerce and as a way to categorize products I've created a field on each Store Category document type that uses the Ucomponents Multi Node Tree Picker to allow the user to select any pages that are of the Product document type. Currently my code is as follows:
@using TeaCommerce.Umbraco.Web
@using TeaCommerce.Api.Models
@using TeaCommerce.Api.Common
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
//Store id
long storeId = long.Parse( Model._Store );
//Product
string ProductTypeName = "Product";
string StoreCategoryName = "StoreCategory";
DynamicNodeList categories = ( Model as DynamicNode ).Descendants( p => p.NodeTypeAlias == StoreCategoryName && p.Level == Model.Level + 1 );
DynamicNodeList products = ( Model as DynamicNode ).Descendants( p => p.NodeTypeAlias == ProductTypeName );
decimal numberOfProducts = products.Items.Count;
decimal numberOfColumns = 3;
decimal rows = Math.Ceiling( ( numberOfProducts / numberOfColumns ) );
int position = 0;
int allProductsPosition = 0;
int tabIndex = 0;
Currency currentCurrency = TC.GetCurrentCurrency( storeId );
}
@foreach ( DynamicNode category in categories ) {
@Html.Raw(string.Format("<div class=\"store-category-panel\">"));
@Html.Raw(string.Format("<div class=\"productList\">"));
position = 0;
foreach ( var product in category.productsPicker ) {
decimal colcount = ( ( position + numberOfColumns ) % numberOfColumns ) + 1;
decimal rowcount = Math.Floor( position / numberOfColumns ) + 1;
string cssClass = string.Format( "product {0} col{1} row{2} ", product.NodeTypeAlias, colcount, rowcount );
cssClass += rowcount == 1 ? " firstRow" : "";
cssClass += rowcount == rows ? " lastRow" : "";
cssClass += colcount % numberOfColumns == 0 ? " lastCol" : "";
string id = "product" + product.Id;
if ( position % numberOfColumns == 0 ) {
@Html.Raw(string.Format("<div class=\"product-list-row\">"));
}
<div class="@cssClass" id="@id">
@RenderPage( "~/macroScripts/product/list-view.cshtml", product, currentCurrency )
</div>
if (colcount % numberOfColumns == 0 || numberOfProducts == (position + 1) ) {
@Html.Raw(string.Format("<div class=\"clearfix\"></div>"));
@Html.Raw(string.Format("</div>"));
}
position++;
}
@Html.Raw(string.Format("<div class=\"clearfix\"></div>"));
@Html.Raw(string.Format("</div>"));
@Html.Raw(string.Format("</div>"));
}
However, I'm getting an error on the line of the nested foreach statement that says "'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'productsPicker' and no extension method 'productsPicker' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)" productsPicker is the alias of the Multi Node Tree Picker field that is on my category document type.
Hi Anthony,
Try to replace
@foreach (DynamicNode category in categories)
with@foreach (dynamic category in categories)
Jeavon
That worked perfectly! Thanks so much that was driving me nuts yesterday.
Ah cool, I would also recommend your replace your
(Model as DynamicNode)
withCurrentModel
is working on a reply...