Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

MVC Razor

Nick Hills edited this page Jun 21, 2017 · 6 revisions

MINQ has been designed to work seamlessly with MVC, it supports the same model syntax you will be familiar with in normal MVC such as:

@Html.TextBoxFor(model => model.Title)

Can be used for common Sitecore outputting requirements:

@Sitecore.LinkFor(model => model.Faq)

Which can be tailored using more advanced syntax than normal MVC:

@Sitecore.LinkFor(c => c.SampleItem.Goto).IfEmpty(() =>
   Sitecore.FieldFor(c => c.SampleItem.Title, new { @class = "fred" }))

If you want to push children from a loop into .FieldFor then you can use the .Helper extension e.g.:

@foreach (var listItem in Model.Items)
{
	<li>@Sitecore.Helper(listItem).FieldFor(l => l.ItemText)</li>
}

A full example:

@using Minq.Mvc;

@model MinqDemo.Model.HomeModel

Simple field:
<div>
@Sitecore.FieldFor(c => c.SampleItem.Title)
</div>

Image:
<div>
@Sitecore.ImageFor(c => c.SampleItem.Graphic, new { width = "40px" })
</div>

Link:
<div>
@Sitecore.LinkFor(c => c.SampleItem.Goto).IfEmpty(() =>
   Sitecore.FieldFor(c => c.SampleItem.Title, new { @class = "fred" }))
</div>

Edit frame:
public class NewsArticlesModel
{
    public IEnumerable<NewsArticleModel> Articles { get; set; }
}
....
<div>
@Sitecore.PageEditor(
    @<ul>
        @foreach (var article in Model.Articles)
        {
            <li>@Sitecore.Helper(article).FieldFor(b => b.Title)</li>
        }
    </ul>
    ,
    new { title = "News Articles", buttons = "/sitecore/content/Applications/WebEdit/Edit Frame Buttons/NewsArticles" }
    )
</div>
Clone this wiki locally