U4-2967 - Inline macros error out when trying to access the Model

Created by Sebastiaan Janssen 27 Sep 2013, 10:30:22 Updated by IanGrainger 02 Jun 2015, 10:38:53

<umbraco:Macro runat="server" language="cshtml">

@Model.Id

</umbraco:Macro>

Errors in trace:

umbracoMacro Error Loading Razor Script (file: ) Cannot perform runtime binding on a null reference at CallSite.Target(Closure , CallSite , Object ) at ASP._Page_App_Data_TEMP_Razor_inline_7a841b1c729ece6b980d8e4e2f394856_cshtml.Execute() in d:\temp\MvcApplication18\App_Data\TEMP\Razor\inline-7a841b1c729ece6b980d8e4e2f394856.cshtml:line 1 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) 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) 0.335414 0.000419

macro Error loading MacroEngine script (file: , Type: '' Cannot perform runtime binding on a null reference at CallSite.Target(Closure , CallSite , Object ) at ASP._Page_App_Data_TEMP_Razor_inline_7a841b1c729ece6b980d8e4e2f394856_cshtml.Execute() in d:\temp\MvcApplication18\App_Data\TEMP\Razor\inline-7a841b1c729ece6b980d8e4e2f394856.cshtml:line 1 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) 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)

Comments

Joachim Andersen 28 Nov 2013, 09:00:05

I'm still able to reproduce this problem in Umbraco 7 Final.

Using the following code: <umbraco:Macro runat="server" language="cshtml"> Skive Kommune - @Model.Id</ </umbraco:Macro></p> <p>I get the following traceback:</p> <p>2013-11-28 09:54:36,063 [5] WARN umbraco.macro - [Thread 179] Error loading MacroEngine script (file: , Type: ''. Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at ASP._Page_App_Data_TEMP_Razor_inline_6caf66c0f07f9e69c3e9d3b3e71381ac_cshtml.Execute() in App_Data\TEMP\Razor\inline-6caf66c0f07f9e69c3e9d3b3e71381ac.cshtml:line 1 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)</p> </p> </span> <hr /> <span id="comment=67-11167"> <small> Sebastiaan Janssen 28 Nov 2013, 10:03:53 </small> <p><p>I think there's some confusion here. If you use v7, out of the box it comes with MVC rendering on. This means that your templates are already views and you don't need to add the macro. Just type something like:</p> <h1>@CurrentPage.Id</h1> {code} <p>Through Model it would also work but you need to go through Model.Content </p> <h1>@Model.Content.Id</h1> {code} <p>For more information make sure to have a look in the documentation on Querying in MVC mode. <a href="http://our.umbraco.org/documentation/Reference/Templating/Mvc/">http://our.umbraco.org/documentation/Reference/Templating/Mvc/</a></p> <p>If this is still a problem in WebForms mode, feel free to re-open this issue.</p> </p> </span> <hr /> <span id="comment=67-11169"> <small> Joachim Andersen 28 Nov 2013, 10:12:27 </small> <p><p>I'm using Webforms so I guess its still a problem.</p> </p> </span> <hr /> <span id="comment=67-11170"> <small> Shannon Deminick 28 Nov 2013, 10:15:28 </small> <p><p>Is this definitely v7 only? Does it work in the latest 6 version?</p> </p> </span> <hr /> <span id="comment=67-11171"> <small> Joachim Andersen 28 Nov 2013, 10:18:06 </small> <p><p>It works in 6.1.4, we haven't tried in the latest v6.</p> </p> </span> <hr /> <span id="comment=67-11172"> <small> Sebastiaan Janssen 28 Nov 2013, 10:21:54 </small> <p><p>Yup, works in 6.1.6 as well. And just reproduced this is indeed an issue in v7 webforms mode. @Joachim A little advice though: if you are going to be using Razor then it's nicer to switch to MVC templates. Give this post a read, it's not so difficult: <a href="http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx">http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx</a></p> </p> </span> <hr /> <span id="comment=67-11377"> <small> Shannon Deminick 06 Dec 2013, 00:22:46 </small> <p><p>All fixed in 77592499637e96d6e19a7e9abf520297d9bee466</p> </p> </span> <hr /> <span id="comment=67-12199"> <small> Andrew Munro 29 Jan 2014, 15:53:44 </small> <p><p>Hey Shannon - I'm having this exact issue with non-inline razor macros. After upgrading to 7.0.0 all macros were broken. Now in 7.0.1, my inline macros work but any macros with defined CSHTML files don't work with this exact exception. At the moment we are in dire need of a fix. Any ideas?</p> <p>See forum post here: <a href="http://our.umbraco.org/forum/developers/api-questions/47934-Model-object-is-null-on-some-razor-macros-in-701">http://our.umbraco.org/forum/developers/api-questions/47934-Model-object-is-null-on-some-razor-macros-in-701</a></p> <p>EDIT: I should mention that we are running in WebForms mode by default.</p> </p> </span> <hr /> <span id="comment=67-12342"> <small> Shannon Deminick 09 Feb 2014, 23:39:28 </small> <p><p>@Andrew, this issue is closed. If you can replicate this issue please create a new issue with the steps and we'll fix</p> </p> </span> <hr /> <span id="comment=67-20904"> <small> IanGrainger 02 Jun 2015, 10:38:53 </small> <p><p>Well, this is fun! I had this issue today after a 7 upgrade.</p> <p>In order to fix it I changed the code to something simple to see why Model was null. Then eventually as I changed it back I ended up with <em>the same code</em>. So I tried just changing the code, then changing it back <em>and it works</em>!?</p> </p> </span> <hr /> </div> <div class="col-xxs-3" style="background-color: #f1efef;"> <div style="padding-left: 6px; padding-right: 6px;"> <p>Priority: Normal</p> <p>Type: Bug</p> <p>State: Fixed</p> <p>Assignee: Shannon Deminick</p> <p>Difficulty: Normal</p> <p>Category: </p> <p>Backwards Compatible: True</p> <p>Fix Submitted: </p> <p>Affected versions: 7.0.0</p> <p>Due in version: 7.0.1</p> <p>Sprint: </p> <p>Story Points: </p> <p>Cycle: </p> </div> </div> </div> </div> <script src="/DependencyHandler.axd?s=L0Fzc2V0cy9qcy92ZW5kb3IvanF1ZXJ5LTIuMS40Lm1pbi5qczsvQXNzZXRzL2pzL3Jlc3BvbnNpdmUubWluLmpzOw&t=Javascript&cdv=651763247" type="text/javascript"></script> </body> </html>