Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Using Archetype for displaying microdata in Umbraco

namespace Microdata.Models
{
    public class MicrodataModel
    {
        public MicrodataModel(string title, string description, string currencyCode, string itemPrice)
        {
            Title           = title;
            Description     = description;
            CurrencyCode    = currencyCode;
            ItemPrice       = itemPrice;
        }
        public string Title         { get; set; }
        public string Description   { get; set; }
        public string CurrencyCode  { get; set; }
        public string ItemPrice     { get; set; }
    }
}
@using Archetype.Extensions
@using Archetype.Models
@inherits UmbracoTemplatePage
<h1>Products</h1>
@Html.RenderArchetypePartials(Model.Content.GetPropertyValue<ArchetypeModel>("products"))
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Archetype.Models;
using Microdata.Models;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Mvc;
namespace Microdata.Controllers
{
    public class HomeController : SurfaceController
    {
        private const string PartialViewFolder  = "~/Views/Partials/Archetype/";
        private const string PartialSubFolder   = "/ArchetypeHelpers/";
        public ActionResult Index()
        {
            IPublishedContent services = CurrentPage;
            ArchetypeModel service = services.GetPropertyValue<ArchetypeModel>("products");//Name given to Archetype in DocType 
            List<MicrodataModel> model = new List<MicrodataModel>();
            foreach (ArchetypeFieldsetModel content in service.Where(x => x.Alias == "displayDemoProductsArchetype")) 
            {
                foreach (ArchetypeFieldsetModel nestedContent in content.GetValue<ArchetypeModel>("productsForSale")) // Name given to nested Archetype in displayDemoProductsArchetype
                {
                    string title        = nestedContent.GetValue<string>("Title");
                    string description  = nestedContent.GetValue<string>("Description");
                    string currencyCode = nestedContent.GetValue<string>("CurrencyCode");
                    string price        = nestedContent.GetValue<string>("ItemPrice");
                    model.Add(new MicrodataModel(title, description, currencyCode, price));
                }
            }
                return PartialView($"{PartialViewFolder}{PartialSubFolder}pvProducts.cshtml", model);
        }
    }
}
@inherits UmbracoViewPage<Archetype.Models.ArchetypeFieldsetModel>
@{ Html.RenderAction("Index", "Home"); }
 
@inherits UmbracoViewPage<List<Microdata.Models.MicrodataModel>>
@*pvProducts*@
<div class="container">
    <div class="case-border">
        <div class="row">
            @foreach (var service in Model.ToList())
            {
                <div class="col-md-6">
                    <section>
                        <article class="service-border">
                            <div itemscope itemtype="http://schema.org/Service">
                                <h1 itemprop="name">@service.Title</h1>
                                <p itemprop="description">@service.Description</p>
                                <p><span itemprop="priceCurrency">@service.CurrencyCode</span><span itemprop="price">@service.ItemPrice</span></p>
                            </div>
                        </article>
                    </section>
                </div>
            }
        </div>
    </div>
</div>

Please enter your comment.