Copied to clipboard

Flag this post as spam?

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


  • Nathan Reece 62 posts 376 karma points
    Jul 30, 2020 @ 07:50
    Nathan Reece
    0

    Can i pass Values into my Umbraco 8 Partial Views

    Is it possible to pass objects into partial views, If so can anyone help me out.

    I have a list of featured staff profiles saved on my home page and i want access to them in my partial view.

    Please can someone help!!

  • David Armitage 510 posts 2082 karma points
    Jul 30, 2020 @ 08:02
    David Armitage
    101

    Hi Nathan,

    Yes this is possible. You can either do it as standard MVC. https://stackoverflow.com/questions/20799658/how-can-i-pass-parameters-to-a-partial-view-in-mvc-4

    For Umbraco what I do is.

    Change the inherits line at the top on your partial view from its default

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    to Something like either of these depending on what you want to do.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<string>
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<int>
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<YourClass>
    

    Then in your view page you can call the partial view liek this.

    @Html.Partial("~/views/partials/yourPartial.cshtml", YourObjectHere)
    

    So going off your description if your are handling featured staff profiles I assume you are dealing with a list of IPublishedContent.

    Your partial should look something like this.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedContent>>
    

    Then your view might look something like this.

    @Html.Partial("~/views/partials/staffProfiles.cshtml", Model.FeaturedStaffProfiles)
    

    Hope this helps.

    Regards

    David

  • Nathan Reece 62 posts 376 karma points
    Jul 31, 2020 @ 05:12
    Nathan Reece
    1

    Thanks Dave, I still dont understand it 100% but i now have my Data passed in and displayed how i intended.

    I have wrote a quick blog to help anyone else with the same issues as i had, Here is a link to my Blog : https://www.umbrajobs.com/blog/posts/2020/july/passing-objects-into-a-partial-view-with-umbraco-8/

  • David Armitage 510 posts 2082 karma points
    Jul 31, 2020 @ 05:14
    David Armitage
    0

    It sounds like you are on the right track.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies