Nested content, news detail , bews archive, search page - FadiZahhar/umbraco8showandtell GitHub Wiki
Create a Document Type: "News Article"
-
title: Textstring
-
summary: Textbox
-
bodyText: Richtext Editor
-
publishDate: Date Picker
-
thumbnail: Media Picker
-
relatedLinks (optional, Nested Content, see below)
Create a Document Type: "News"
-
No properties required, unless you want an intro, banner, etc.
-
Under Permissions, allow "News Article" as a child.
Property: relatedLinks
-
Type: Nested Content
-
Element Type: "Link"
-
caption: Textstring
-
url: Url Picker
-
This allows editors to add a repeatable list of links per article.
Suppose you have a Nested Content property alias relatedLinks
.
In your NewsArticle.cshtml view:
@{
var relatedLinks = Model.Value<IEnumerable<IPublishedElement>>("relatedLinks");
}
@if (relatedLinks != null && relatedLinks.Any())
{
<section class="related-links">
<h3>Related Links</h3>
<ul>
@foreach(var link in relatedLinks)
{
<li>
<a href="@link.Value("url")" target="_blank">
@link.Value("caption")
</a>
</li>
}
</ul>
</section>
}
What’s happening?
-
Model.Value<IEnumerable<IPublishedElement>>("relatedLinks")
fetches the Nested Content list. -
You loop and render each as a list item.
File: /Views/NewsArticle.cshtml
@inherits Umbraco.Web.Mvc.UmbracoViewPage
<article class="news-article">
<header>
<h1>@Model.Value("title")</h1>
<time datetime="@Model.Value<DateTime>("publishDate"):yyyy-MM-dd">
@Model.Value<DateTime>("publishDate"):dddd, MMM d, yyyy
</time>
</header>
@if (Model.HasValue("thumbnail"))
{
var thumb = Model.Value<IPublishedContent>("thumbnail");
<img src="@thumb.Url()" alt="@Model.Value("title")" class="img-fluid" />
}
<div class="news-body">
@Model.Value("bodyText")
</div>
@* Render related links via nested content *@
@{
var relatedLinks = Model.Value<IEnumerable<IPublishedElement>>("relatedLinks");
}
@if (relatedLinks != null && relatedLinks.Any())
{
<section class="related-links">
<h3>Related Links</h3>
<ul>
@foreach(var link in relatedLinks)
{
<li>
<a href="@link.Value("url")" target="_blank">
@link.Value("caption")
</a>
</li>
}
</ul>
</section>
}
</article>
File: /Views/NewsArticles.cshtml
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
var articles = Model.Children()
.Where(x => x.ContentType.Alias == "newsArticle")
.OrderByDescending(x => x.Value<DateTime>("publishDate"));
}
<section class="news-list">
<h1>Latest News</h1>
<ul>
@foreach(var article in articles)
{
<li>
<h2><a href="@article.Url">@article.Value("title")</a></h2>
<small>@article.Value<DateTime>("publishDate"):MMM d, yyyy</small>
@if (article.HasValue("summary")) {
<p>@article.Value("summary")</p>
}
</li>
}
</ul>
</section>
Create SearchController.cs
in your Core project:
using System.Linq;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
public class SearchController : SurfaceController
{
[HttpPost]
public ActionResult Search(string query)
{
if (string.IsNullOrWhiteSpace(query))
{
TempData["searchError"] = "Please enter a search term.";
return RedirectToCurrentUmbracoPage();
}
var results = Umbraco.ContentAtRoot()
.DescendantsOrSelf()
.Where(x => x.ContentType.Alias == "newsArticle" &&
(x.Value<string>("title").InvariantContains(query)
|| x.Value<string>("summary").InvariantContains(query)
|| x.Value<string>("bodyText").InvariantContains(query)))
.ToList();
return PartialView("SearchResults", results);
}
}
File: /Views/Search.cshtml
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@using (Html.BeginUmbracoForm("Search", "Search"))
{
<input type="text" name="query" placeholder="Search news..." class="form-control" />
<button type="submit" class="btn btn-primary">Search</button>
}
@if (TempData["searchError"] != null)
{
<div class="alert alert-danger">@TempData["searchError"]</div>
}
@Html.Action("SearchResults", "Search")
File: /Views/Partials/SearchResults.cshtml
@model IEnumerable<Umbraco.Core.Models.IPublishedContent>
@if (Model != null && Model.Any())
{
<ul>
@foreach(var result in Model)
{
<li>
<a href="@result.Url">@result.Value("title")</a>
<p>@result.Value("summary")</p>
</li>
}
</ul>
}
else
{
<p>No results found.</p>
}
-
Add links to your News listing and Search page in your main navigation partial or layout.
Page | DocType Alias | View File | Description/Notes |
---|---|---|---|
News Article | newsArticle | NewsArticle.cshtml | Shows full article, nested |
News Listing | news | NewsArticles.cshtml | Loops children for archive |
Search | search | Search.cshtml + Partial | Shows search box/results |
Nested Content | any | In NewsArticle.cshtml | Related links, repeatables |
This gives you a complete, scalable approach for nested content, news detail, news archive, and search, all following best practices for Umbraco 8.