Copied to clipboard

Flag this post as spam?

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


  • Damion 96 posts 331 karma points
    Oct 30, 2019 @ 11:10
    Damion
    0

    IPublishedContent is null in my View

    When opening a View from a controller like this

    public ActionResult ViewAccount()
    {
        return View("~/Views/MemberAccount.cshtml");
    }
    

    And the View inherits like this

    @inherits UmbracoViewPage<IPublishedContent>
    

    I'm finding that IPublishedContent in the View is null, therefore I cannot get Dictionary Items.

    IPublishedContent is not null in the main View where this one is opened from.

    Am I missing something here?

  • Jeffrey Veer 41 posts 308 karma points
    Oct 30, 2019 @ 11:33
    Jeffrey Veer
    0

    Hi Damion :-),

    From where are you calling this code? If it's just for a partial you could directly render the partial from your other views, which will result in the model automatically being passed on.

    When you render a view as shown above, you can pass the model as a second parameter:

    public ActionResult ViewAccount()
    {
        return View("~/Views/MemberAccount.cshtml", viewModel);
    }
    
  • Damion 96 posts 331 karma points
    Oct 30, 2019 @ 11:50
    Damion
    0

    Hi,

    I'm calling it from

    @{Html.RenderAction("ViewAccount", "MemberAccountPage", Model);}
    

    The view this is on inherits from UmbracoViewPage<IPublishedContent>, which I assumed (maybe wrongly) would make the content available as the model, is that not the case?

    Interestingly, every partial opened from the homepage has access to all the content, it's only when I'm opening an entirely different view that I see this.

  • Jeffrey Veer 41 posts 308 karma points
    Oct 30, 2019 @ 12:05
    Jeffrey Veer
    1

    Hi,

    I'm wondering why it's working on the Homepage, as the code you provided sets the Model explicitly to null in the Controller it inherits from.

    Can you try rendering the View directly as a partial instead of using the Action? As you don't have any functionality in the controller for now.

    @Html.Partial("~/Views/MemberAccount.cshtml"); // You can optionally add a parameter for the model. By default the current Model will be passed on.
    

    P.S. instead of wrapping the Html.RenderAction in @{...} you can also use Html.Action directly from the html.

  • Damion 96 posts 331 karma points
    Oct 30, 2019 @ 12:26
    Damion
    1

    Ok, what you've said has given me a thought.... Going to try something and I'll let you know how it goes after lunch (important stuff first!)

  • Damion 96 posts 331 karma points
    Oct 30, 2019 @ 14:07
    Damion
    0

    Right, turn out I copied the wrong line into the post and there is some functionality to retrieve member data in the partial. The full sequence is

    An href to "@Url.Action("ViewAccount", "MemberAccount")"

    which does this return View("~/Views/MemberAccount.cshtml");

    within that page, @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent> and 3 partials are rendered (one of which mentioned above has some functionality), but it's at this point that IPublishedContent is null.

    All 3 partials use dictionary items, and all are missing.

    Think I'll bin it and try it from scratch

Please Sign in or register to post replies

Write your reply to:

Draft