0

I have a view that renders a side bar for a content page. The side bar can be rendered several times on a single page. The page's layout defines several content sections (or, positions, in Kooboo's terminology) which are, in an actual page, bound to specific views. In each such view I need to optionally render a side bar by redering the general-purpose side bar view.

Side bars' content is stored in Kooboos database. I'm using a specific content type which has, for the sake of simplicity, the following two fields:

  • Position — the ID of the position in the page, which matches an ID of a position defined in the layout
  • Content — an HTML fragment that constitute's the side bar's actual content

The side bar view's logic should be as follows: from all side bar data bound to the current page select a single item with a Position matching a position passed from the outer view as an argument, and, if found, render it. The problem is, I can't figure out a way to pass arguments into a view

Sample layout fragment:

<!-- Header section -->
<section>
    <div class="section-content">
        @Html.FrontHtml().Position("Header")
        @Html.FrontHtml().Position("HeaderSectionContent")
    </div>
</section>

<!-- Main section -->
<section class="even">
    <div class="section-content">
        @Html.FrontHtml().Position("MainSectionContent")
    </div>
</section>

Sample view bound to the Header position:

<div class="header-container">
    <h1 class="page-title" @ViewHelper.Edit(ViewBag.Solution, "Title")>
        @ViewBag.Solution.Title
    </h1>

    <!-- How to pass "Header" string as an argument into the Controls.Sidebar view? -->
    @Html.FrontHtml().RenderView("Controls.Sidebar",ViewData)

    <h2 @ViewHelper.Edit(ViewBag.Solution, "Perex")>
        @Html.Raw(ViewBag.Solution.Perex)
    </h2>
</div>

The intended general purpose side bar view Controls.Sidebar:

@if (ViewBag.Sidebars != null) 
{
    // How to retrieve an argument and select the matching side bar data?
    var sidebar = ViewBag.Sidebars ... ;
    if (sidebar != null)
    {
        <div class="side-bar">
            <h3>@item.Title</h3>
            @Html.Raw(item.Content)
        </div>
    }   
}

At the end of the day I need to be able to render this view several times, and conditionally render several side bars in respect to data availability.

I was looking into the Parameters definitions available in the view editor, but it seems this provides access to query string parameters, which is not a mechanism I could (would like to) leverage. Kooboo documentation is lacking any information related to this topic.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90

1 Answers1

3

There are at least two ways to pass some parameters from one view into another in Kooboo:

First you can store your parameter value in the parent view like this:

    Context.Items["ParameterName"]=value;

or like this:

    var customViewData = new ViewDataDictionary();
    customViewData.Add("ParameterName", value);

Then you can call rendering of a child view:

    @Html.FrontHtml().RenderView("ChildViewName", ViewData) //a default ViewData passed

or as following, respectively:

    @Html.FrontHtml().RenderView("ChildViewName", customViewData)

On the receiving side in ChildViewName you simply fetch the parameter value either thus:

    var value = Context.Items["ParameterName"];

or thus, respectively:

    var value = ViewData["ParameterName"]; // NB: don't forget to cast parameter to a proper data-type, because ViewData is not processed same smoothly as ViewBag would. 

Both ways work just fine for this purpose.