Skip to main content

Umbraco Razor Crops - One way of checking if the Crop exists

This bit of code (highlighted yellow) writes out the Count of a cropped image as a comment, if it fails a default image is written out.

@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.template;

@using System.Xml.XPath

@{




int foundMatch = 0;
string outPutText = "";


foreach (var page in Model.Children)
{
var thisImage = page.homePageImage;
var mediaItem = Model.MediaById(@thisImage);

 

if (@mediaItem.CroppedImages.Count() > 0)
  {
<hr />
@Html.Raw("<article class=\"\">");

<div>


<div class="one_half first">
<a href="@page.Url" title="@page.navigationName">

@try
{
@Html.Raw("<!--" + @mediaItem.CroppedImages.Find("@name", "mediumImage").Count() + "-->");
<img alt="@page.Name" src="@mediaItem.CroppedImages.Find("@name", "mediumImage").url" />
}
catch
{ <img alt="@page.Name" src="/media/117165/default-video-image.jpg" />
}
</a></div>


<div class="one_half">
@if (@page.navigationName != "")
{
<h2>@page.navigationName</h2>
}
<p>
@if (@page.teaserText.Length >= 130)
{
@page.teaserText.Substring(0,130) @Html.Raw("...");
}
else
{
@page.teaserText;
}
</p>
<footer class="read-more"><a href="@page.Url" title="@page.pageHeading">Read More &raquo;</a></footer>
</div>


</div>
@Html.Raw("</article><div class=\"clear\"> </div>");


<hr />
  }



}
}




Comments

Popular posts from this blog

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

This is a Simple Contact From done in C# .Net and also can be used in Umbraco.  Paste the code in to Visual Web Developer for a better view. The important bits are coment in the second file.  One of the Commented out lines highlights how to get the mailto address from an Umbraco field. The code Below includes the myContactForm.ascx.cs codebehind file and the myContactForm.ascx file. 1) The myContactForm.ascx file:  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactFormforPublications.ascx.cs" Inherits="usercontrols_ContactFormPublications" %> <div class="contact-form" id="contactForm" runat="server">     <h3>Request a Publication</h3>     <div class='int'>         <form runat="server">         <asp:Panel DefaultButton="contactFormSubmit" runat="server">         <fieldset>             <div class='

Umbraco 7 - C# Razor - Passing a parameter variable from a Template to a Partial View.

Passing a parameter variable from a Template to a Partial View Example - Passing a Page Id to a Partial View. In the example below we are passing 3 page Ids (1090. 1091, 1092) to a partial view.  The Partial View will display the details for each of these nodes on a home page.  See final output below. 1) In your Template: @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1090}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1091}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1092}}) 2) The Partial View: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var thisPageId = 1090; var stringPage = @ViewData["pageId"]; thisPageId = Convert.ToInt32(stringPage); } <div class="col-md-4"> <arti