Table of Contents

ContextAttributes in Razor-Output

Technically the entire Edit-UI is JavaScript based, so all the buttons, events etc. are client side scripts.

These scripts need to know what Context they are in, meaning which Dnn-Module, which 2sxc-App, which Zone, permissions etc.

By default, this context is already provided by the environment, but sometimes a new context must provide overrides. This is rare, but important, for example using Inner Content (Content Within Other Content).

So the ContextAttributes will provide this information in some hidden html.

Discover More in the Razor Tutorials

We have an rich series of Razor tutorials. You should really check them out 👍.


How it works

The Edit.ContextAttributes(...) is always used inside an HTML-tag and will add some attributes with JSON. Any buttons or actions inside that tag will then find this information, and assume that it is has precendence over the global information.

What do You need to do?

By default, this context is already provided by the 2sxc-environment, but sometimes a new context must provide overrides. For example using Inner Content (Content Within Other Content).

For this you need the Edit.ContextAttributes - see docs here.

How to use

This example shows the title and will add the standard editing-buttons for the Content item.

Here's an Inner Content (Content Within Other Content) example:

<div class="app-blog-text sc-content-block-list" @Edit.ContextAttributes(post, field: "DesignedContent")>
    @foreach(var cb in @post.DesignedContent) {
        @cb.Render();
    }
</div>

In this example, the Edit.ContextAttributes will add some attributes with JSON, which will help the toolbars inside that loop to correctly edit those items, and not the main item around it.

How it works

The Edit.ContextAttributes(...) is always used inside an HTML-tag and will add some attributes with JSON. Any buttons or actions inside that tag will then find this information, and assume that it is has precendence over the global information.

Using ContextAttributes

These context-attributes enhance an HTML-tag, so that buttons inside that tag can be in a different context than the original context.

Here's a common example: imagine you have a 2sxc-instance (a module showing 2sxc-data) and all the buttons there know the App-ID, the Zone, the Content-Type etc. Inside this module, you can have multiple items but they all still work well in the original context (all items are in the same app, so an edit-dialog will also know the AppId).

Read also

You should find some code examples in this demo App

History

  1. Introduced in 2sxc 8.4