Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Garrett Fisher 341 posts 496 karma points
    Nov 06, 2015 @ 12:36
    Garrett Fisher
    0

    Razor Not Working Globally

    Hi,

    Developed a site locally (7.3) and I'm doing all my nullchecks and plugging in all my template values using Razor macros a la:

    <umbraco:Macro  runat="server" language="cshtml">
    @if(Model.HasValue("ctaButtonLinkUrl")) {
    <a href="@Model.ctaButtonLinkUrl">@Model.ctaButtonText</a>
    }
    </umbraco:Macro>
    

    Everything working great. Then I put the templates up on a staging environment and I get the following error on every single @Model reference:

    Cannot perform runtime binding on a null reference.
    

    This is a bummer. Seems like it must be a configuration issue. Can anyone think of a reason why I may not be able to "speak" Razor in my templates? Code in the page is identical to that on my local instance.

    Thanks,

    Garrett

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Nov 06, 2015 @ 12:42
    Dennis Aaen
    0

    Hi Garrett,

    You are running Umbraco 7.3, did you run in MVC mode or in WebForms mode. If you are running in MVC mode, then you should try something like this.

    I assume that you are using a content picker to pick the url to the page.

    @{
      if (CurrentPage.HasValue("ctaButtonLinkUrl")){
        var node = Umbraco.Content(CurrentPage.ctaButtonLinkUrl);
        <a href="@node.Url">@node.Name</a>
      }
    }
    

    Hope this helps,

    /Dennis

  • Garrett Fisher 341 posts 496 karma points
    Nov 06, 2015 @ 13:02
    Garrett Fisher
    0

    Thanks for your speedy reply, Dennis.

    Actually I am running in WebForms mode because some other developers on the project are using umbraco:item tags -- however, on all our local instances we are able to use both techniques in our templates. I have umbraco:item tags and Razor macros in the same page and it's working fine. I am not using a URL picker but just a textfield (it's an external link), but I went with your idea and syntax:

    @{
      if (CurrentPage.HasValue("ctaButtonLinkUrl")){
        <a href="@CurrentPage.ctaButtonLinkUrl">@CurrentPage.ctaButtonText</a>
      }
    }
    

    And I get:

    The name 'CurrentPage' does not exist in the current context.
    

    Thanks again for your help. Any other ideas?

    //Garrett

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Nov 06, 2015 @ 13:13
    Dennis Aaen
    0

    Hi Garrett,

    When you are running in WebForms, as I remember it´s not possible to add Razor directly into the templates.

    Could you try create a partial view or a partial view macro with this code, and place it on your template where you want to output the data.

    @{
      if (CurrentPage.HasValue("ctaButtonLinkUrl")){
        <a href="@CurrentPage.ctaButtonLinkUrl">@CurrentPage.ctaButtonText</a>
      }
    }
    

    To see if this works for you.

    Hope this helps,

    /Dennis

  • Garrett Fisher 341 posts 496 karma points
    Nov 06, 2015 @ 14:27
    Garrett Fisher
    0

    Well, the proof is in the pudding -- it is "possible to add Razor directly into the templates." I'm doing it locally. The *.cshtml macros only don't work on this server. I suspect I'll get the same error if using a partial view but I will try it later! Thanks again for staying with me on this! Cheers.

    //Garrett

Please Sign in or register to post replies

Write your reply to:

Draft