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 67 posts 310 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 131 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 67 posts 310 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 537 posts 1222 karma points
    Apr 13, 2020 @ 08:06
    Sebastian Dammark
    1

    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"];
    
Please Sign in or register to post replies

Write your reply to:

Draft