Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to display blocks properties by looping over them in the page view, rather than outputing them in the blocks view?

Vote:
 

We have a simple block (MyBlock) with a few string properties, and a view which simply outputs the properties as text.

e.g.  

{  "title": "@Html.PropertyFor(x => x.title)" }

We have a page with a content area where you can create/drag/drop several of these blocks.

The view for the page simply outputs the content area thus:

@Html.PropertyFor(x => @Model.MyBlockContentArea)

We get:

{ "title":"some title" }

{ "title":"some title2" }

{ "title":"some title3" }

This works, but we now need to output an order field in each block, and a comma after all but the last.

{ "title":"some title", "order":1 },

{ "title":"some title2", "order":2 },

{ "title":"some title3", "order":3 }

Presumably, in the page view, we need a way to itterate over the blocks in the content area, and output them programatically, and not render anything in the individual blocks views (i.e. the block has no view, it is only every viewed via its page).

Any idea the simplest way to do this?

#224718
Jun 24, 2020 15:21
Vote:
 

Quick and dirty:

@if (Model.CurrentPage.MainContentArea != null)
{
    for (var i = 0; i < Model.CurrentPage.MainContentArea.FilteredItems.Count(); i++)
    {

        @Html.Raw("{ \"title\":\"" + Model.CurrentPage.MainContentArea.FilteredItems.ElementAt(i).GetContent().title + "\", \"order\":" + i + "\" }");
        if (i < Model.CurrentPage.MainContentArea.FilteredItems.Count() - 1)
        {
            @Html.Raw(",<br/>");
        }        
    }
}
#224719
Edited, Jun 24, 2020 15:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.