Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jurgen Van de Water 18 posts 68 karma points
    Feb 18, 2015 @ 16:54
    Jurgen Van de Water
    0

    Reuse umbraco grid with custom viewmodel

    I'm new to the new umbraco grid functionality and I have a question about it.

    I have a page with an article where I use the grid functionality to show some image and text besides that image. This is working fine.

    In the properties of that page I have put a checkbox for saying that the article is for sale or not, when checked the product should automatically appear on a for sale page.

    I can get the product that is for sale on that page, but now I also want to show the image and text on that page. I've created a List with a custom viewmodel to hold the content for every product that is for sale. The problem is with providing the JObject to the GetGridHtml function. This function wants a string and I'm providing the property which results in an error.

    What is the best way to get the content for the grid to show on that separate page?

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
         var home = CurrentPage.Site();
    
         List<ForSaleItemsViewModel> forSaleItemsVM = new List<ForSaleItemsViewModel>();
    
         if(home.Children.Any())
         {        
             foreach(var childPage in home.Children)
             {
                 if(childPage.Children.Any())
                 {
                     foreach(var child in childPage.Children)
                     {
                         if(child.HasProperty("ForSale") && child.ForSale)
                         {
                               forSaleItemsVM.Add(new ForSaleItemsViewModel
                                                {
                                                    ID = 1,
                                                    Content = child.Content,
                                                    Sold = child.Sold
                                                });
                         }
                    }
                }
             }
        }
    }
    
    <div class="container">
        @foreach(var item in forSaleItemsVM)
        {
            if (item.Sold)
            {
                <span>SOLD!</span>
            }
            CurrentPage.GetGridHtml(item.Content.ToString(), "bootstrap3");
        }
    </div>
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 18, 2015 @ 17:46
    Dennis Aaen
    100

    Hi Jurgen,

    If you see the documentation for best pratices for the grid layout https://our.umbraco.org/Documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors-v7/Grid-Layout-Best-Practices there is a chapter about the limitations for the grid layout https://our.umbraco.org/Documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors-v7/Grid-Layout-Best-Practices#Limitations. And of the limitions of using the grid layout is that it´s not for reused content.

    It says - There is no managed api to drill into the grid content and target specific cell content - so a grid layout is not a recommended storage of reusable content - it simply wasn't designed for this scenario. If you wish to reuse content in multiple pages, it is still recommended that you store these pieces of content as seperate content nodes, so they can be stored, cached and queried as usual.

    /Dennis

  • Jurgen Van de Water 18 posts 68 karma points
    Feb 18, 2015 @ 18:51
    Jurgen Van de Water
    0

    Thanks for the answer Dennis.

    Then the grid system is in fact just for editors to easily add content on specific places without the need of a developer. It is a nice system but then I cannot use it on my website, because several articles can be for sale and needs to be visible on the for sale page.

    Also thanks for the links, I will read them trough.

Please Sign in or register to post replies

Write your reply to:

Draft