Note that there are some explanatory texts on larger screens.

plurals
  1. POUmbraco Razor - Script fails using Children.Random
    primarykey
    data
    text
    <p>I am having a weird issue, hoping you all have some pointers. I am using Umbraco 4.11.10 and trying to fulfill a requirement to display 1 random quote from a collection of quotes. The really puzzling thing is that this exact code works on a different site using the same version of Umbraco.<br> The Quote object only has two properties...quoteText and quoteSignature. Here is my entire razor script (which currently fails).</p> <pre><code>@inherits umbraco.MacroEngines.DynamicNodeContext @{ var item = @Model.NodeById(1113).Children.Random(); &lt;div&gt; &lt;blockquote&gt;@item.quoteText&lt;span&gt;- @item.quoteSignature&lt;/span&gt;&lt;/blockquote&gt; &lt;/div&gt; } </code></pre> <p>If I strip out the div and block quote and all of that and just leave</p> <pre><code>var item = @Model.NodeById(1113).Children.Random(); </code></pre> <p>it still fails.</p> <p>If I remove Random() and output the @item variable it is a DynamicNodeList. If I do something like..</p> <pre><code>foreach(var child in item) { &lt;div&gt; &lt;blockquote&gt;@child.quoteText&lt;span&gt;- @child.quoteSignature&lt;/span&gt;&lt;/blockquote&gt; &lt;/div&gt; } </code></pre> <p>It works, but of course I get all of the quotes. So I guess that at least proves that the data is valid, and the property names are valid. Just seems to be an issue using Random(). As I am pretty new to Razor I'm hoping this is just an oversight on my part.</p> <p>*<em>UPDATE *</em> The error from the error log is as follows</p> <pre><code>2013-09-03 19:34:00,142 [11] WARN umbraco.macro - [Thread 24] Error loading MacroEngine script (file: CFRandomQuote.cshtml, Type: ''. Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Random' at CallSite.Target(Closure , CallSite , Object , Int32 ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at ASP._Page_macroScripts_CFRandomQuote_cshtml.Execute() in c:\inetpub\cf\macroScripts\CFRandomQuote.cshtml:line 5 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage) at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage) at umbraco.macro.loadMacroScript(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) </code></pre> <p>Thanks Everyone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload