Copied to clipboard

Flag this post as spam?

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


  • Rick 36 posts 150 karma points
    Jul 01, 2021 @ 18:52
    Rick
    0

    Umbraco v8 - Getting Current Node Id

    Hey All,

    I'm missing something simple and just can't seem to put my finger on it. (Umbraco v8.13.0)

    I've got a partial view with a custom model and this view is shared/rendered in different areas of the site. This view has some functionality (buttons) that I may or may not want to display depending on where the view is rendered. Seems like I should be able to say if its node id 111, 222, then true else false kind of thing. But I just can't seem to get the node id for the life of me.

    In the view, I've tried changing the @model MVC directive to: @inherits UmbracoViewPage

    Then I tried it on the Controller side to just dump the id into the model I'm sending to the view trying:

    int myId = Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id; ...and...

    IPublishedContent nodeInfo = Umbraco.AssignedContentItem; results.myId = nodeInfo.Id;

    Typically my error is: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent. I am using VS and the auto-complete indicates that its finding the references fine.

    I just feel like fighting this where it's just something simple that I'm missing. Maybe a constructor or using statement?

    Thank you in advance, Rick

  • Chris Evans 137 posts 353 karma points c-trib
    Jul 01, 2021 @ 23:49
    Chris Evans
    0

    Without seeing a bit more of your code it's a little difficult to see what's happening here.

    Is your controller inheriting from Umbraco.Web.Mvc.SurfaceController? if so, then you should be able to access the UmbracoHelper natively just like this:

    Umbraco.AssignedContentItem.Id
    

    Likewise, if your view is an UmbracoViewPage you should also have a handle to the context of the Umbraco page being rendered so the same code should work there too.

    Feel free to post more of your code in its entirety as that should help determine what might be preventing these helpers from becoming available.

  • Rick 36 posts 150 karma points
    Jul 02, 2021 @ 12:01
    Rick
    0

    Chris,

    Thank you for the quick reply.

    Below is a snippet from the Partial View and the resulting error message. Let me know if you see anything obvious that I'm missing.

    @inherits UmbracoViewPage<NameChanged.Customizations.Models.AssetDetailModel_EC2>
    @using NameChanged.Customizations.Models;
    @using NameChanged.Customizations.Classes;
    
    @{
        IEnumerable<AssocProjectsModel> assocProjects = AssetData.GetAssocProjects(Model.AssetId);
        IEnumerable<AssocAssetsModel> assocAssets = AssetData.GetAssocAssets(Model.AssetId);
        int nodeId = Umbraco.AssignedContentItem.Id;
    }
    

    I get a 500 error on the AssignedContentItem.Id line stating the following:

    Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.

    Without the AssignedContentItem line the view renders without any issues. I'll try again on the Controller side and post what I run into there.

    Thank you again, Rick

  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Jul 02, 2021 @ 12:33
    Patrick de Mooij
    0

    Hi Rick,

    How do you render your Partial? It seems like it's rendered outside of the Umbraco context (hence why it's constructed without a published content)

  • Rick 36 posts 150 karma points
    Jul 02, 2021 @ 13:17
    Rick
    0

    Patrick,

    That's likely the issue and there is a piece of the puzzle that I'm just missing.

    On the page(s), there are two panes of data being displayed. The top pane is a summary table. The user clicks on a row in that table which triggers an ajax call to my controller to fetch the details. On the controller side, I'm making a call to my SQL database and returning a model with the data.

    Controller Snippet:

    //--------------------------------------------------------------------
    //- GetDetails_EC2
    //---------------------------------------------------------------------
    [HttpPost]
    public ActionResult GetDetails_EC2(int assetId)
    {
        AssetDetailModel_EC2 results = AssetData_EC2.GetAssetDetails_EC2(assetId);
        return PartialView("Partials/AssetDetails_EC2", results);
    }
    

    Maybe I can't do it on the view side and need to do it on the Controller side which is fine, but I can't get it to work there either. I feel like I'm fighting it which is typically an indication that I'm doing it wrong and just missing something.

    Thanks in advance for any insight you can provide.

    Rick

  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Jul 02, 2021 @ 13:24
    Patrick de Mooij
    100

    Yes, what is going wrong here is the fact that an API controller has no idea about the current state of the user. So it doesn't know what page the request came from, what language etc... Hence why the error states that it has no IPublishedContent.

    So in this case, you have to pass something that allows you to get the current page. The easiest way to do this is to pass the id of the current page to the API controller and then use that to get the page.

  • Rick 36 posts 150 karma points
    Jul 02, 2021 @ 14:47
    Rick
    0

    Patrick,

    Thank you. Not the solution that I was hoping for, but it does resolve my issue.

    For anyone else coming across this, I just added a data attribute to the page title ([email protected]), then before my ajax call I grab it ($('h2').first().data('nodeid')) and pass it over to the Controller as another parameter.

    It does mean that I have to update all of the models to send the value back with the data, but it is what it is.

    Thanks again for the assistance, Rick

Please Sign in or register to post replies

Write your reply to:

Draft