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 70 posts 313 karma points c-trib
    Apr 13, 2020 @ 05:09
    Mikael Axel Kleinwort

    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);


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

    Defining the view instead as

    @model IEnumerable<PublishedElementModel> 


    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 132 karma points
    Apr 13, 2020 @ 06:31
    Dhanesh kumar mj

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


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


    Html.RenderPartial("your view", your_model);

    Please see here for more info

    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


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


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


    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 547 posts 1287 karma points
    Apr 13, 2020 @ 08:06
    Sebastian Dammark

    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 292 posts 978 karma points
    1 week ago

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

Please Sign in or register to post replies

Write your reply to: