Skip to main content
Home  › ... Razor

Entity (Item) Tutorials

Tutorial HomeEntity

Working with Entity (Item) Values

Requirements

Every item a.k.a. object is called an Entity. Entities will have properties like Name, ProductNumber or Birthday, depending on the Content-Type they represent.

How your Razor Code receives Entities

When things are configured correctly, the right entities are given to your Razor-template so you can just show them or perform calculations as you need them. These are the common ways for the entities to be provided to your template:

  1. On simple templates, the UI allows users to add/edit content of a specific type (configured in the View). This content Entity is always available in your code in the variable called Content.
  2. On templates which expect many items of the same type, Content contains the first item only, and to access all (for iterating and showing each one) the Entities are in a variable called Data.
  3. If your code needs to find other data in the App, it can access it through App.Data.
  4. If your code want to get Entities which have been pre-selected/filtered etc. through a query, it will get them from Query

In this example the template is configured to contain an Entity of the type Person and has these fields:

  • FirstName: a string (text)
  • LastName: a string
  • Birthday: a date
  • Mugshot: a link to an image file
  • Awards: a reference to one or more other Entities which describe awards

So the following examples will show how to put the values in these fields into the HTML. The current item is always available on the variable called Content. It's a dynamic object, so you can just type things like @Content.FirstName to access the properties.

Show Entity Values

Output

  • Name: Douglas Adams
  • Birthday: 3/11/1952
  • Award: Hugo Award
<img loading="lazy" src="@Content.Mugshot?w=100&h=100&mode=crop" class="rounded-circle">
<ul>
  <li>Name: @Content.FirstName @Content.LastName</li>
  <li>Birthday: @Content.Birthday.ToString("d")</li>
  <li>Award: @Content.Awards.Name</li>
</ul>

Note that Awards refers to other Entities of the type PersonAwards and has properties like Name. The above example showed the award Name using @Content.Awards.Name - which makes sense when you only expect one award. In other tutorials you'll see how to work with such related Entities if there are more than one.

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->


<h2>Working with Entity (Item) Values</h2>
<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
      <p>
        Every <em>item</em> a.k.a. <em>object</em> is called an <a href="https://docs.2sxc.org/specs/data/entities.html" target="_blank"><strong>Entity</strong></a>. 
        Entities will have properties like <code>Name</code>, <code>ProductNumber</code> or <code>Birthday</code>, depending on the <a href="https://docs.2sxc.org/specs/data/content-types.html" target="_blank"><strong>Content-Type</strong></a> they represent. 
      </p>
      <h3>How your Razor Code receives Entities</h3>
      <p>
        When things are configured correctly, the right entities are given to your Razor-template so you can just show them or perform calculations as you need them. These are the common ways for the entities to be provided to your template:
      </p>
      <ol>
        <li>On simple templates, the UI allows users to add/edit content of a specific type (configured in the View). This content Entity is always available in your code in the variable called <code>Content</code>. </li>
        <li>On templates which expect many items of the same type, <code>Content</code> contains the first item only, and to access all (for iterating and showing each one) the Entities are in a variable called <code>Data</code>.</li>
        <li>If your code needs to find other data in the App, it can access it through <code>App.Data</code>.</li>
        <li>If your code want to get Entities which have been pre-selected/filtered etc. through a query, it will get them from <code>Query</code></li>
      </ol>
      <p>
        In this example the template is configured to contain an Entity of the type <code>Person</code> and has these fields:
      </p>
      <ul>
        <li>FirstName: a string (text)</li>
        <li>LastName: a string</li>
        <li>Birthday: a date</li>
        <li>Mugshot: a link to an image file</li>
        <li>Awards: a reference to one or more other Entities which describe awards</li>
      </ul>
      <p>
        So the following examples will show how to put the values in these fields into the HTML. The current item is always available on the variable called <code>Content</code>. It's a <code>dynamic</code> object, so you can just type things like <code>@@Content.FirstName</code> to access the properties. 
      </p>
    </div>
  </div>


<h3>Show Entity Values</h3>

  <img loading="lazy" src="@Content.Mugshot?w=100&h=100&mode=crop" class="rounded-circle">
  <ul>
    <li>Name: @Content.FirstName @Content.LastName</li>
    <li>Birthday: @Content.Birthday.ToString("d")</li>
    <li>Award: @Content.Awards.Name</li>
  </ul>



Note that Awards refers to other... <!-- unimportant stuff, hidden -->

@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })