5

In my DXA view, i'm rendering a region using this code

@if (Model.Regions.ContainsKey("RegionName"))
{
    @ViewBag.Renderer.RenderRegion(Model.Regions["RegionName"], Html)
}

This works fine. If my page's component presentations contain a template for this specific region, they get rendered here, along with the region XPM tag.

However, let's say I want to render an empty region in my view, to let content editors drag and drop new content in this region using XPM.

In that case, my page object won't contain a region with key "RegionName", so my region will not be rendered. However, the RenderRegion function expects a region model object to be passed along. What's the best way to do this? Should we then just call the RenderRegion method with a new region object, or is there another recommended way to do this ?

@if (Model.Regions.ContainsKey("RegionName"))
{
    @ViewBag.Renderer.RenderRegion(Model.Regions["RegionName"], Html)
}
else
{
    @ViewBag.Renderer.RenderRegion(new Region(){Name="RegionName"}, Html)        
}

1 Answer 1

5

Indeed, for now that's what you will have to do.

Note that the API is rather verbose currently (you have to put quite some logic in your view code). For the next version of DXA, we're envisioning a much more concise syntax, which can also deal with empty/non-existent regions. Something like @Html.Region("RegionName", renderEmpty: true)

Note that this is using an extension method on ASP.NET MVC's HtmlHelper Class; something you could do yourself in your implementation too (to keep your view code cleaner).

3
  • Thanks for the info. An HTML helper seems like the best option indeed Commented Apr 22, 2015 at 3:27
  • I think your region still needs to be rendered, for which you will need a controller/view in the Region.AppData
    – Will Price
    Commented Apr 22, 2015 at 10:31
  • Indeed, so you will have to specify the name of the View to use in case the given Region is empty/non-existent. By default, this is the same as the region name (if it's a View from the Core). Commented Apr 24, 2015 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.