November Happy Hour will be moved to Thursday December 5th.

Shared block cannot contains itself?

Vote:
 

Hi, we have met quite unexpected problem with shared blocks.

Scenario assumes that we have shared block which provides some ContentArea with specific layout and we would like to allow users to nest itself to provide maximum flexibility in content managment.

I define this block:

    public class TwoColumnLayoutBlock : BaseLayoutBlock
    {
        [Display(
            Name = "Left Column",
            Description = "Content of left column",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        public virtual ContentArea LeftColumn { get; set; }

        [Display(
            Name = "Right Column",
            Description = "Content of right column",
            GroupName = SystemTabNames.Content,
            Order = 2)]
        public virtual ContentArea RightColumn { get; set; }
    }

With default Controller and this View:

@model MyProject.Models.Layouts.TwoColumnLayoutBlock

@{ Html.RenderContentArea(Model.LeftColumn); }
@{ Html.RenderContentArea(Model.RightColumn); }

Than I create Two instances of TwoColumnLayoutBlock in CMS named "MainLayout" and "LeftColumn" and I put LeftColumn in MainLayout.LeftColumn property as content.

If I try to add MainLayout to my main page content area I get some ugly error:

   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.Stack`1.Peek()
   at System.Web.WebPages.WebPageBase.GetOutputWriter()
   at System.Web.WebPages.WebPageExecutingBase.BeginContext(String virtualPath, Int32 startPosition, Int32 length, Boolean isLiteral)
   at ASP._Page_addons_MyProject_Views_TwoColumnLayoutBlock_Index_cshtml.Execute() in c:\work\src\myproject\Views\TwoColumnLayoutBlock\Index.cshtml:line 12
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)

I would be very grateful if someone could point me some errors in my approach. In theory there is some recurence going on, but in fact LeftColumn block does not reference any other block, so it seems a little bit strange to me.

Sincerely

Maciej Błaszczyk

#131697
Aug 03, 2015 16:00
Vote:
 

As far as I remember there were some naming issues on how WebPages are building rendering context for the current item to render. Try to rename TwoColumnLayoutBlock from "LeftColumn" to anything else. Have a working sample in alloy - Block1, main instance "asdf", 2 other child instances "qwer1" and "qwer2". I can put "qwer1" in "asdf" left column and "qwer2" in left column.

From content area rendering perspective there shouldn't be any issues regarding cascade hierachy of the blocks - they are just yet another content to resolve, select and render.

#131707
Aug 03, 2015 22:52
Vote:
 

Thank you for your response Valdis.

I tried to change names for some random convention and I still have this issue.

My current tree looks like that:

  • Home (PageData with ContentArea)
    • qwerty123 (TwoColumnLayoutBlock with two content areas)
      • asdf890 (TwoColumnLayoutBlock) DOES NOT WORK!

And still my error seems the same. Stack trace points the .cshtml View for TwoColumnLayoutBlock line which tries to access to Model.LeftColumn property.

Strangely if I remove asdf890 from qwerty123 ContentArea, everything works fine.

If I add to qwerty123 ContentArea instance of other Block (not TwoColumnLayoutBlock), it works fine too!

  • Home (PageData with ContentArea)
    • qwerty123 (TwoColumnLayoutBlock with two content areas)
      • zxcv567 (RowLayoutBlock) WORKS FINE!
#131718
Aug 04, 2015 10:04
Vote:
 

Hello again.

We have found the issue. It was hidden in excessive StructureMap container intialization, but Error Output did not point it in any way :/

However thank you Valdis for your help :)

We can mark this as solved and closed.

#131735
Aug 04, 2015 13:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.