Fork me on GitHub
2sxc Learn Razor
DotNetNuke tools to create, design and animate great content
You are here: Home  >  Relationships

Using Entity-Relationships with Razor

Since 2SexyContent 5.3 it offers entity-relationships. This means that some sets of information - like a quote - can be related to one or more pieces of information - like authors. With razor you can easily access the related information and use it to create great stuff. Here's what you see on this page:

  1.  A course description showing you info about
    1. the related author incl. photo and link to his site - all data from the related entity
    2. a list of categories - since more than one can be assigned
  2. A list of authors showing all courses they made
  3. A list of categories

Course: 2SexyContent Lists-Training

A short video introduction to using lists
Categories: 2SexyContent, DNN / DotNetNuke, Razor, jQuery,

Course: 2SexyContent Relationships Training

This training helps you understand relationships in 2SexyContent
Categories: Razor, 2SexyContent,

Course: DNN Video Training

A video course to show how DNN works for beginners.
Categories: DNN / DotNetNuke, HTML 5,

Course: 2SexyContent EAV and Responsive Design

A complex course showing the internals of the EAV (Entity Attribute Value) system inside 2SexyContent and how to leverage it for mobile solutions.
Categories: 2SexyContent, Razor, HTML 5,

Daniel Mettler

Visit his website here

Courses by Daniel Mettler

  1. 2SexyContent Lists-Training

Benjamin Gemperle

Visit his website here

Courses by Benjamin Gemperle

  1. 2SexyContent EAV and Responsive Design

Raphael Müller

Visit his website here

Courses by Raphael Müller

  1. DNN Video Training
  2. 2SexyContent Relationships Training

Course List by Category (A-Z)

  • 2SexyContent
    1. 2SexyContent EAV and Responsive Design by Benjamin Gemperle
    2. 2SexyContent Lists-Training by Daniel Mettler
    3. 2SexyContent Relationships Training by Raphael Müller
  • DNN / DotNetNuke
    1. 2SexyContent Lists-Training by Daniel Mettler
    2. DNN Video Training by Raphael Müller
  • HTML 5
    1. 2SexyContent EAV and Responsive Design by Benjamin Gemperle
    2. DNN Video Training by Raphael Müller
  • Razor
    1. 2SexyContent EAV and Responsive Design by Benjamin Gemperle
    2. 2SexyContent Lists-Training by Daniel Mettler
    3. 2SexyContent Relationships Training by Raphael Müller

Look inside

Content Item

These are the values a content-editor can manage.
Name Type Value
Title System.String 2SexyContent Lists-Training
Description System.String A short video introduction to using lists
Author ToSic.Eav.Data.EntityRelationship 1548
Categories ToSic.Eav.Data.EntityRelationship 1552,1551,1553,1581


Presentation Item

These are additional, optional presentation instructions a content-editor can manage. If none are entered, a default set (predefined by the designer) will be used.

No Presentation parameters specified, using default presentation for this item.


Template file

<div class="sc-element">
  @Html.Raw(Content.Toolbar)
  <div>
    <h3>Course: @Content.Title</h3>
    <div>@Content.Description</div>

	@{	// Special Comment
		// When a new item is added you might not have any data at all
		// then a access to the property Author 
		// would give you a small text telling you that it doesn't exist
		// so to check if there is any data in the relationship, 
		// just check if it's not a string...
		// ...&& check if the count > 0 to see if anything is assigned
		// since relationships are always lists
		if(Content.Author.GetType() != typeof(String) && Content.Author.Count > 0) { 
			var Author = Content.Author[0]; 
			<div>Author: 
				<a href="@Author.Website" target="_blank">
					@Author.FullName 
					<img src="@Author.Photo?w=30&h=30&mode=crop&s.roundcorners=100">
				</a>
			</div>
		}
	}
    @if(Content.Categories.Count > 0) {  // check if anything is in the list
      <div>Categories: 
        @foreach(var cat in Content.Categories) {
            @cat.Category<text>, </text>
        }
      </div>
    }

  </div>
</div>

Look inside

Content Item

These are the values a content-editor can manage.
Name Type Value
FullName System.String Daniel Mettler
Website System.String http://www.2sic.com/dm
Photo System.String /learn-razor/Content/Authors/Daniel.jpg


Presentation Item

These are additional, optional presentation instructions a content-editor can manage. If none are entered, a default set (predefined by the designer) will be used.

No Presentation parameters specified, using default presentation for this item.


Template file

@* loop through the list of authors *@
@foreach(var e in List) {
	// get the content entity (not the presentation)
	var Content = e.Content;

	// Here's some special stuff necessary to find all courses referring to this author
	// 1. leave the simple API, go to the power API of entities, must be typed to work
	ToSic.Eav.Interfaces.IEntity ent = AsEntity(Content);
	
	// 2. Get all incomming relationships (all parents)
	var entitiesReferringToThis = ent.Relationships.AllParents;
	
	// 3. Filter all parents, we only want the courses (and not books, etc.)
	var Courses = entitiesReferringToThis.Where(c => c.Type.Name == "Courses");
	
	<div class="sc-element">
	  @Html.Raw(Content.Toolbar)
	  <img src="@Content.Photo?w=200&h=200&mode=crop" style="float:left">
	  <div style="height: 220px">
			<h3>@Content.FullName</h3>
			<p>Visit his website <a href="@Content.Website" target="_blank">here</a></p>
			@if(Courses.Any()) {
				<p>
				<strong>Courses by @Content.FullName</strong><br>
				<ol>
					@foreach (var course in Courses) 
					{
						var c = AsDynamic(course);
						<li>@c.Title</li>
					}
				</ol>
				</p>
			}
	  </div>
	</div>
}

Look inside

Content Item

These are the values a content-editor can manage.
Name Type Value
Category System.String DNN / DotNetNuke


Presentation Item

These are additional, optional presentation instructions a content-editor can manage. If none are entered, a default set (predefined by the designer) will be used.

No Presentation parameters specified, using default presentation for this item.


Template file

<div>
	<h2>Course List by Category (A-Z)</h2>
	<ul>
	@foreach(var e in List.OrderBy(e => e.Content.Category)) {
		var Content = e.Content;

		// Here's the short version for the list of course-parents
		var Courses = ((ToSic.Eav.Interfaces.IEntity)AsEntity(Content))	// cast to use power-API
			.Relationships.AllParents	// all parents, no matter what type
			.Where(c => c.Type.Name == "Courses");	// now only the courses
	
		<li class="sc-element">
			@Html.Raw(Content.Toolbar)
			@Content.Category
			@if(Courses.Any()){
				<ol>
					@foreach(var course in Courses.OrderBy(c => AsDynamic(c).Title)) {
						var Course = AsDynamic(course);
						<li>@Course.Title by 
							@if(Course.Author.GetType() != typeof(String) && Course.Author.Count > 0){
								<em>@Course.Author[0].FullName</em>
								<img src="@Course.Author[0].Photo?w=30&h=30&mode=crop&s.roundcorners=100">
							}
						</li>
					}
				</ol>
			}
		</li>
	}
	</ul>
</div>
2serve . 2invent . 2create is 2be.