Copied to clipboard

Flag this post as spam?

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


  • Aximili 177 posts 278 karma points
    Mar 28, 2012 @ 01:34
    Aximili
    0

    Model in cshtml incorrectly referring to the current Page

    In my Content (in Umbraco) I have

    • A "page" for showing all News
    • A "folder" containing NewsItem's
      (NewsItem has a property called Teaser)

    In the page, I have this code

    var allNews = new Node(Static.NODE_ID_NEWS).Children;
    foreach (INode newsItem in allNews)
    {
      string teaserText = umbraco.library.RenderMacroContent(newsItem.GetProperty("teaser").Value, newsItem.Id);

    That renders the following macro (cshtml)

    name = @Model.Name <br />
    id = @Model.Id

    From that, I found out that, when run from the page (ie. frontend), the Model is the page, not the news item. (When previewed from TinyMce editor it correctly showed the NewsItem name and ID)

    How can I get the actual news item from this .cshtml file?
    (I'm also happy to use usercontrol for the macro)

    Thank you in advance.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 28, 2012 @ 10:23
    Michael Latouche
    0

    Hi Hardi,

    Not sure it will help a lot, but have you tried with @DynamicModel instead of @Model?

    Cheers,

    Michael.

  • Aximili 177 posts 278 karma points
    Mar 29, 2012 @ 00:50
    Aximili
    0

    "DynamicModel does not exist in the current context" it says... Thanks anyway :) I'm giving up soon

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 29, 2012 @ 10:16
    Michael Latouche
    0

    Hi Hardi,

    Unfortunately, I don't think I can be of much more help anymore :-S Just one last question: when you say "In the page, I have this code", what do you mean exactly?. Is it in the template? Or in some user control?

    Cheers,

    Michael.

  • Aximili 177 posts 278 karma points
    Mar 30, 2012 @ 00:36
    Aximili
    0

    Sorry, by page I mean the master page (ie. the template, which shows as xxx.aspx on the frontend). Can't edit the question in this forum.

    That's ok, I'm not using macros anymore, and you've helped a lot! =) Thanks again!

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 30, 2012 @ 09:51
    Michael Latouche
    0

    OK, too bad it did not work with macro's, but if you got it working in some other way, it's already something :-)

    Cheers,

    Michael.

  • Gustas Sagaitis 14 posts 93 karma points
    Apr 13, 2012 @ 15:34
    Gustas Sagaitis
    0

    Hi,

    I am trying to pull off same trick. I have macro string (<?UMBRACO_MACRO macroAlias="MyMacroAlias" />) as property value on a node (alias "submenuMacroContent"). 

    <ul>
        @foreach(var child in Model.Children)
        {
            if(child.HasValue("submenuMacroContent"))
            {
            @Html.Raw(umbraco.library.RenderMacroContent(@child.GetProperty("submenuMacroContent").Value, child.Id));
            }
            else
            {
            <li><a href="@child.Url">@child.Name</a></li>
            }
        }
    </ul>
    

    That macro gets rendered in incorrect context. I want it to be rendered in child.Id context but it renders as in Model.Id.

    Maybe its bug?

  • Jonatan 2 posts 22 karma points
    Jun 06, 2013 @ 15:27
    Jonatan
    0

    Hi,

    I found the same problem in our current development.   I was providing a different nodeId than current page, but the macro was always getting Model.ID = current page, not the one I had provided.

    After diving a while through the source code, I found this workaround: modifying directly Context.Current.Items["pageID"].

    NOTE: I'm not sure about the implications of this in different scenarios. What I know is that in my case is working fine.


        @foreach(var child in Model.Children)
        {
            if(child.HasValue("submenuMacroContent"))
            { 
    object originalPageID = HttpContext.Current.Items["pageID"];
    HttpContext.Current.Items["pageID"] = child.Id;


     
    @Html.Raw(umbraco.library.RenderMacroContent(@child.GetProperty("submenuMacroContent").Value, child.Id));

    HttpContext.Current.Items["pageID"] = originalPageID; 
      } else {
    ...
    } }

     

     

     

    I see this post is quite old, but the problem remains unsolved.

    Hope this helps,

    Jonatan

Please Sign in or register to post replies

Write your reply to:

Draft