Copied to clipboard

Flag this post as spam?

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


  • Silvan Egger 28 posts 200 karma points
    Feb 24, 2016 @ 08:41
    Silvan Egger
    0

    Call a Partial View with a Parameter

    Hi everyone

    I have a document type "Person" and i want to create a partial view, where i can give a dynamic amount of persons, which should then be displayed.

    I tried it like this:

    My Template:

    @{
        //i get the persons from the multinode treepicker
        var persons = Umbraco.Content(Umbraco.Field("persons").ToString().Split(','));
    }
    
    <p>these persons are involved:</p>
    
    @Html.Partial("showPersons", (DyanmicPublishedContentList)persons)
    

    And this is my Partial View:

    @model DynamicPublishedContentList
    
    <ul>
        @foreach(var person in Model)
        {
            <li>@Model.GetPropertyValue("personPrename") @Model.GetPropertyValue("personSurname")</li>
        }
    </ul>
    

    I get this error:

    Compiler Error Message: CS0246: The type or namespace name 'DynamicPublishedContentList' could not be found (are you missing a using directive or an assembly reference?)

    This doesn't work in any Way... I'm looking for a good solution for my case...

    I already did it with calling the partial for every person like this:

    Template:

    @Html.Partial("showPerson", (IPublishedContent)leader)
    

    Partial View:

    @model IPublishedContent
    
    <p>@Model.GetPropertyValue("personPrename") @Model.GetPropertyValue("personSurname")</p>
    

    But i need to be able to display multiple persons with one partial view for design reasons...

    Thanks for your help.

    Greets, Silvan

  • Ryan 34 posts 138 karma points
    Feb 24, 2016 @ 09:00
    Ryan
    100

    Could you not pass in an IEnumerable of IPublishedContent?

    So your partial view will be something like this....

    @model IEnumerable<IPublishContent>
    
    foreach(var person in Model)
    {
       <p>@Model.GetPropertyValue("personPrename") @Model.GetPropertyValue("personSurname")</p>
    }
    
  • Silvan Egger 28 posts 200 karma points
    Feb 24, 2016 @ 09:19
    Silvan Egger
    0

    Great! This solved my problem, thanks!

    The solution now looks like this

    Template

    @{
        //I get the persons from the multinode treepicker
        var persons = Umbraco.Content(Umbraco.Field("persons").ToString().Split(','));
    }
    
    <p>these persons are involved:</p>
    
    @Html.Partial("showPersons", (IEnumerable<IPublishedContent>)persons)
    

    Partial View

    @model IEnumerable<IPublishedContent>
    
    <ul>
        @foreach(var person in Model)
        {
            <li>@person.GetPropertyValue("personPrename")</p>
        }
    </ul>
    

    Greets, Silvan

Please Sign in or register to post replies

Write your reply to:

Draft