Copied to clipboard

Flag this post as spam?

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


  • Thomas Ravnholt 12 posts 102 karma points
    Sep 28, 2022 @ 08:32
    Thomas Ravnholt
    0

    Passing parameters to a partial

    Hello. I'm trying to pass some parameters to a partial, but i can't figure out how to do it it.

    What i want to achieve

    I want to be able to use the same partial multiple times in a template, but pass it the value of different aliases each time.

    lets say i have a partial which renders a textstring and textarea.

    I would like to be able to do something like this

    @await Html.PartialAsync("PartialName", name = "@Model.Value("title"), image = "@Model.Value("text")")
    
    @await Html.PartialAsync("PartialName", name = "@Model.Value("IntroTitle"), image = "@Model.Value("IntroText")")
    

    To render this content

    enter image description here

    If i had an Array of items i would do this, but that is not the case.

    @foreach (var item in collection)
    {
        @await Html.PartialAsync("PartialName", item)
    }
    

    I found this in the docs, but i can't figure out how to make it work.

    https://our.umbraco.com/Documentation/Reference/Templating/Mvc/examples#:~:text=Rendering%20a%20macro%20with%20parameters%20using%20an%20anonymous%20object

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Sep 28, 2022 @ 08:56
    Huw Reddick
    2

    you should be able to pass it as a viewdata object

    new { name = "@Model.Value("IntroTitle"), image = "@Model.Value("IntroText")" }
    

    Then access it in the partial

    ViewData["name"]; 
    
  • Thomas Ravnholt 12 posts 102 karma points
    Sep 28, 2022 @ 09:45
    Thomas Ravnholt
    0

    I cant seem to make that work, im sure im doing something completely wrong :)

    Render:

    @await Html.PartialAsync("PartialName", new { name = "Thomas"})
    

    Inside partial

    var name = ViewData["name"];  
    <p>@name</p>
    
  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Sep 28, 2022 @ 10:26
    Huw Reddick
    102

    I just checked my code and I am doing this to pass the data

    new ViewDataDictionary(this.ViewData) { { "Name", "Checkout" }, { "Header", "Checkout" } }
    

    Then in Partial

    var name = ViewData["Name"].ToString();
    
  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Sep 28, 2022 @ 10:35
    Huw Reddick
    1

    TBH, I have switched most of my partials to use viewcomponents in Umbraco 9+ so most of my partials are just static html, anything requiring data to be passed I have moved to viewcomponents.

    But the example above is still working :)

  • Thomas Ravnholt 12 posts 102 karma points
    Sep 28, 2022 @ 10:51
    Thomas Ravnholt
    0

    Thanks alot! I'll take a look at viewcomponents, sounds like that is a better solution ;)

  • 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