Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    May 08, 2017 @ 14:01
    Dibs
    0

    pass IPublishedContent to partial view

    Hi all just starting to make use of custom controllers in Umbraco and fairly new to MVC, I would like to render content in a partial view via ContentAtXPath i.e var newsArticles = Umbraco.ContentAtXPath("//news"); how do i pass content from ContentAtXPath to the partial view ?

    thanks Dibs

  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    May 08, 2017 @ 14:30
    Frans de Jong
    0

    I'm not entirely shore what you mean.

    Normaly I have a partial view in my template like @Html.Partial("whatever")

    On that partial you could do:

    var newsArticles = Umbraco.ContentAtXPath("//news");
    
    var property = newsArticles.GetPropertyValue("someproperty");
    

    Does that answer your question?

  • Dibs 202 posts 991 karma points
    May 08, 2017 @ 14:43
    Dibs
    0

    Hi Frans

    I would like to make use of my views to only display data/content passed to it rather then get and display in the view.

    I just want to know how to pass the list of new articles or list of any content to a view. Then loop through the content passed to it via a foreach statement

    hope that makes sense

    Thanks Dibs

  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    May 08, 2017 @ 15:11
    Frans de Jong
    0

    In that case you could do the following:

    Template:

    var newsArticles = Umbraco.ContentAtXPath("//news");
    @Html.Partial("myView", newsArticles);
    

    Partial view:

        @model IPublishedContent
    
    @foreach(var item in model.Children)
    {
    //Do stuff
    }
    

    Although I would keep the foreach in the template and pass the child to the partial view like so:

    var newsArticles = Umbraco.ContentAtXPath("//news");
    
    foreach(var article in newsArticles)
    {
        @Html.Partial("myView", article);
    }
    
  • Dibs 202 posts 991 karma points
    May 11, 2017 @ 10:41
    Dibs
    101

    Cheers Frans

    I am now able to render content by creating a model (docType.cs) and add custom property (IEnumerable

    Thanks Dibs

  • 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.

    Continue discussion

Please Sign in or register to post replies