Skip to main content
Home  › ... Razor

Data Tutorial

Tutorial HomeData
The samples can differ based on your Razor base class or if you're running an old version.
Switch to Strong-Typed (2sxc 17.06+) Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)
Requirements
Resources

Basic Example from App.Data

The easiest data to access comes from the current environment - like from the App or from DNN itself. The following example gets Persons data from the App.Data.GetStream("Persons") and just loops through them. This is similar to the example in the content tutorial, except that the data comes from the App.Data instead of the current instance Data.

⬇️ Result | Source ➡️

  • Douglas Adams
  • Terry Pratchett
  • Neil Gaiman
  • George Akerlof
  • Raphael Müller (not an author)
  • Ed Hardy
@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade
@using System.Linq
<ul>
  @foreach (var person in AsItems(App.Data["Persons"])) {
    <li>
      @if (Text.Has(person.Url("Mugshot"))) {
        <img loading="lazy" src='@Link.Image(person.Url("Mugshot"), width: 50, height: 50, resizeMode: "crop")' width="50px" style="border-radius: 50%">
      }
      @person.Get("FirstName")  @person.Get("LastName")
    </li>
  }
</ul>

Queries are pre-build for things like "get all authors and the books of the author in the URL" or similar (more). These are configured in the App administration. The query AuthorsWithBooks (shown to the right) is used in this example. Below you'll see us first iterate through all the items in the Authors Stream. If an author was clicked on (which places the author-id in the url), the query will also return the current Author in the Current stream as well as the CurrentBooks of that author.

@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade
@using System.Linq

@{
  // get the query
  var query = Kit.Data.GetQuery("AuthorsWithBooks");

  // get all authors from the stream "Authors" in the query
  var allAuthors = AsItems(query["Authors"]);

  // get the current author (if available) from the stream Current
  var current = AsItem(query["Current"]);

  // get the books of the current author (if available) from stream CurrentBook
  var books = AsItems(query["CurrentBooks"]);

  // Get the current url params so we can derive from it for details links
  var urlParams = MyPage.Parameters;
}
<ul>
  @foreach (var person in allAuthors) {
    <!-- this li will have class=selected if it's the current one -->
    <li class='@(person.Equals(current) ? "selected" : "")'>
      <!-- this creates a link to the current page and author=id -->
      <a href='@Link.To(parameters: urlParams.Set("authorId", person.Id))'>
        @person.Get("FirstName") @person.Get("LastName")
      </a>
    </li>
  }
</ul>
@if (current != null) {
  if (current.IsNotEmpty("Mugshot")) {
    <img loading="lazy" src="@current.Url("Mugshot")?w=100&h=100&mode=crop" width="100px" class="person float-right float-end">
  }
  <h3>Current Author: @current.Get("FirstName") @current.Get("LastName")</h3>
  <strong>Books</strong>
  <ol>
    @foreach (var book in books) {
      <li>@book.Get("Title")</li>
    }
  </ol>
} else {
  <h3>No author selected - click on an author above</h3>
}

View Configuration

This is how this view would be configured for this sample.

  • Query: AuthorsWithBooksDnn
Details for Query Authors with Books

Get Authors with Books data