Copied to clipboard

Flag this post as spam?

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


  • Mikael Axel Kleinwort 154 posts 499 karma points c-trib
    Apr 13, 2020 @ 05:09
    Mikael Axel Kleinwort
    0

    Simple way to pass parameter to partial view with Umbraco 8

    What's the simplest way to pass a parameter to a partial view in Umbraco 8 ? I am (still) inexperienced with MVC and I am grateful for some insight.

    I want to pass a IEnumerable<PublishedElementModel> parameter (the value of a a nested content property) to a partial view.

    Using Macro Partials doesn't work out of the box. IEnumerable<PublishedElementModel> is not available as a macro parameter type.

    This is my view definition:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.NestedContent>
    

    the model type NestedContent has a nested content property MyNestedContentProperty.

    With the partial view inPageNav defined as

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

    calling the partial like

    var items = Model.MyNestedContentProperty as IEnumerable<PublishedElementModel>;
    @Html.Partial("inPageNav", items);
    

    throws

    Cannot bind source type Umbraco.Web.PublishedModels.NestedContent to model type System.Collections.Generic.IEnumerable`1[[Umbraco.Core.Models.PublishedContent.PublishedElementModel, Umbraco.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null]].

    Defining the view instead as

    @model IEnumerable<PublishedElementModel> 
    

    throws

    The model item passed into the dictionary is of type 'Umbraco.Web.PublishedModels.NestedContent', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Umbraco.Core.Models.PublishedContent.PublishedElementModel]'.

    I know that I don't really understand how it is supposed to work. I am grateful for some hints in the right direction. Thank you!

  • Dhanesh kumar mj 18 posts 134 karma points
    Apr 13, 2020 @ 06:31
    Dhanesh kumar mj
    100

    Hey Mikael, You can simply pass the Model to a Partial View

    1)

    @model exampl.Models..Example
    <div class="row-fluid">
    
             @Html.Partial("~/Views/example/_examplePartial.cshtml", Model)
    
    </div>
    

    2)

    @{
    Html.RenderPartial("your view", your_model);
    }
    

    Please see here for more info

    https://our.umbraco.com/Documentation/Reference/Templating/Mvc/partial-views

    https://www.jondjones.com/learn-umbraco-cms/umbraco-7-tutorials/umbraco-and-mvc/how-to-use-mvc-partials-in-umbraco/

    And from your code, I can see that the error is throwing because of Passing Model and binding models are different.

    Cannot bind source type Umbraco.Web.PublishedModels.NestedContent to model type System.Collections.Generic.IEnumerable1[[Umbraco.Core.Models.PublishedContent.PublishedElementModel]].

    Please use the same Model for both passing and binding

    IF the above is not working, please use the dynamic way

    here

    @Html.Partial("YourPartialName", (object)CurrentPage.example)
    

    //PartialView

    @model dynamic
    var custommodel= (object)Model
    
  • Mikael Axel Kleinwort 154 posts 499 karma points c-trib
    Apr 13, 2020 @ 07:17
    Mikael Axel Kleinwort
    0

    Danesh,

    thank you for your answer. Now I get it :-)

    I was thinking too complicated. I got it working in no time thanks to your advice.

    Very cool. Mikael

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 13, 2020 @ 08:06
    Sebastian Dammark
    4

    You can also pass parameters in a ViewDataDictionary like this:

    @Html.Partial("PartialView.cshtml", Model, new ViewDataDictionary { { "parameter1", parameter1 }, { "parameter2", parameter2 } })
    

    Here I'm passing the Model, and 2 parameters into the PartialView.

    In the PartialView you can then do like this to access the parameters:

    var parameter1 = ViewData["parameter1"];
    
  • bh 444 posts 1544 karma points
    Apr 05, 2021 @ 19:12
    bh
    1

    @sebastiandammark this post has helped me a great deal. Thank you for sharing!

  • Sten Hougaard 12 posts 84 karma points
    May 11, 2023 @ 09:57
    Sten Hougaard
    0

    Always ready with a great answer, Sebastian :-)

    I however would like to share a version which uses a bit more code, as I had issues with doing it using your inline version. Maybe other developers will also get the same issues as me and could use this version.

    @{
    var viewData = new ViewDataDictionary(ViewData);
    viewData.Add("parameter1", parameter1);
    }
    @Html.Partial("PartialView", Model, viewData)
    
    1. I did the creation of viewData in a code block
    2. I used the PartialView name, not including the .cshtml extention
  • pbl_dk 150 posts 551 karma points
    Apr 11, 2024 @ 12:56
    pbl_dk
    0

    Same problem but now for Umbraco 13:

    Page
    @await Html.PartialAsync("Site/NewsArticles", Model, new ViewDataDictionary(ViewData) { { "parameter1", "test" } }
    
    
    Inside partial view
    @ViewData["parameter1"]
    
  • 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