Copied to clipboard

Flag this post as spam?

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


  • Martin 114 posts 313 karma points
    Jan 27, 2015 @ 18:09
    Martin
    0

    IPublishedContent and accessing content fields in Partial view

    Hi,

    I have a created Partial view for displaying the latest news item on the start page of my site. On the actual news page I use Currentpage in order to access all the news field and display them in a list. However, the start page wont work reusing the code so I tried a IPublishContent approach as shown below:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    
    <ul class="nyhetslista">
        @*OrderBy() takes the property to sort by*@
    
    @{
        var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");
        if (locationNodes.Any())
        {    
            foreach (var publishedContent in locationNodes.First().Children)
            {
    
                <p>@publishedContent.Id</p>
                foreach (var page in publishedContent.Children)
                {
                <p>@page.Id</p>
    
                }
            }
        }
    }
    </ul>
    

    @page.Id works fine, but I cant figure out how to reference the actual content fields. For example, if I try @page.huvudrubrik (where "huvudrubrik" is the alias of a textfield in the content page), I get this error message:

    Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Huvudrubrik' and no extension method 'huvudrubrik' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    Can somebody help me?

    Best regards, /Martin

    Below is the working partial view for the news page if it could be of any help in understanding my problem:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
      @*      <li><a href="@page.Url">@page.Name</a></li>  *@
    
    <ul class="nyhetslista">
        @*OrderBy() takes the property to sort by*@
        @foreach (var page in CurrentPage.Children.OrderBy("nyhetsdatum descending"))
        { 
            <ul>
            <li>@page.nyhetsdatum.ToString("d MMM yyyy")</li>
            <li>@page.huvudrubrik</li>
            <li>@page.underrubrik</li>
            <li>@page.bodyText</li>
            <hr class="line_gradient" />
            <br>
            </ul>
        }
    </ul>
    
  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 27, 2015 @ 18:14
    Dan Lister
    1

    Hi Martin,

    You will have to use the GetPropertyValue() method as follows in order to retrieve the correct property value:

    @Model.Content.GetPropertyValue<string>("huvudrubrik");
    

    Thanks, Dan.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2015 @ 18:17
    Dennis Aaen
    1

    Hi Martin,

    What if you do something like this does this a difference.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>

    <ul class="nyhetslista">
        @*OrderBy() takes the property to sort by*@

    @{
        var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");
        if (locationNodes.Any())
        {   
            foreach (var publishedContent in locationNodes.First().Children)
            {

                <p>@publishedContent.Id</p>
                foreach (var page in publishedContent.Children)
                {
                <p>@page.GetPropertyValue<string>("huvudrubrik")</p>

                }
            }
        }
    }
    </ul>

    If not then try to add @using Umbraco.Web;

    Hope this helps,

    /Dennis

  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 27, 2015 @ 18:21
    Dan Lister
    0

    Also Martin, from your first code example, I notice you have two inherits statements. From what I know, I think your view can only inherit from one. The last being used in your first example. It might be worth checking to see if thats correct.

    Thanks, Dan.

  • Martin 114 posts 313 karma points
    Jan 27, 2015 @ 20:24
    Martin
    0

    Unfortunately, it still does not work.

    When adding <p>@page.GetPropertyValue<string>("huvudrubrik")</p> I get this error:

    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

    Source Error:

    Line 20: foreach (var page in publishedContent.Children) Line 21: { Line 22:

    @page.GetPropertyValue

    Line 23: Line 24: }

    I also added @using Umbraco.Web;but no change. It seem strange, as the content page node Id is displayed correctly, the page fields should be available too...

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2015 @ 20:33
    Dennis Aaen
    1

    Hi Martin,

    What if you try something like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    <ul class="nyhetslista">
    @{
        var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");
        if (locationNodes.Any()){   
            foreach (var publishedContent in locationNodes.First().Children){

                <p>@publishedContent.Id</p>
                    foreach (var page in publishedContent.Children){
                        <p>@page.GetPropertyValue<string>("huvudrubrik")</p>
                    }
            }
        }
    }
    </ul>

    Hope this helps,

    /Dennis

  • Martin 114 posts 313 karma points
    Jan 27, 2015 @ 20:49
    Martin
    0

    It didn't unfortunately, I still get this error, where line 10 is red:

    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

    Source Error:

    Line 8:

    @publishedContent.Id

    Line 9:
    foreach (var page in publishedContent.Children){ Line 10:

    @page.GetPropertyValue

    Line 11:
    } Line 12: }

    Do you know if it possible to rewrite my "Currentpage" partial in a way so that Currentpage is replaced with the proper content page instead? In order to get a non-dynamic reference.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2015 @ 20:52
    Dennis Aaen
    0

    Hi Martin,

    Think I found what goes wrong. We have missed the @ in front of the second for each loop

    @inheritsUmbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    <ul class="nyhetslista">
    @{
       
    var locationNodes =Model.AncestorOrSelf(1).Descendants("NyheterMain");
       
    if(locationNodes.Any()){    
           
    foreach(var publishedContent in locationNodes.First().Children){

               
    <p>@publishedContent.Id</p>
                    @foreach (var page in publishedContent.Children){
                        <p>@page.GetPropertyValue<string>("huvudrubrik")</
    p>
                   
    }
           
    }
       
    }
    }
    </ul>

    Hope this helps,

    /Dennis

  • Martin 114 posts 313 karma points
    Jan 27, 2015 @ 21:09
    Martin
    0

    Thanks for your effort but I got a different error. It says that @ is not needed as I have declared a code section on the 3rd row and havn't left that section yet.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2015 @ 21:22
    Dennis Aaen
    0

    Hi Martin,

    With this code I donĀ“t get any erros could you please see if it woks for you too.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    <ul class="nyhetslista">
    @{
        var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");
        if (locationNodes.Any()){   
            foreach (var publishedContent in locationNodes.First().Children){

                <p>@publishedContent.Id</p>
                    foreach (var page in publishedContent.Children){
                     <p>@page.GetPropertyValue("huvudrubrik")</p> 
                    }
            }
        }
    }
    </ul>

    /Dennis

  • Martin 114 posts 313 karma points
    Jan 28, 2015 @ 09:38
    Martin
    0

    Hi Dennis,

    I didn't get any error but I didn't get any output either.

    I call the partial using @Html.Partial("NyheterFront", @Model.Content) but I guess that passing @Model.Content is correct.

  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 28, 2015 @ 09:40
    Dan Lister
    0

    Hi Martin,

    I'd try removing the second @ character as it isn't necessary:

    @Html.Partial("NyheterFront", Model.Content)
    

    Thanks, Dan.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 28, 2015 @ 09:44
    Dennis Aaen
    0

    Hi Martin,

    And maybe you need to change the

    var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");

    To

    var locationNodes = Model.Content.AncestorOrSelf(1).Descendants("NyheterMain");

    Hope this helps,

    /Dennis

  • Martin 114 posts 313 karma points
    Jan 28, 2015 @ 10:20
    Martin
    0

    Sorry to say it didn't help, I got a different error complaining about "Content". I think I have to look into a different approach to make this work. If I'll work it out, I'll let you know.

    Thanks!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 28, 2015 @ 12:08
    Jeroen Breuer
    0

    Hello,

    If the news is different on the start page why do you want to use a partial view? Can't you just do your coding on the homepage template instead of a partial view?

    It might also help to check out the Hybrid Framework. It has some best practises for Umbraco 7.

    Jeroen

  • Martin 114 posts 313 karma points
    Jan 28, 2015 @ 15:31
    Martin
    2

    SOLVED IT!

    It found a nice solution in the TXT Starter Kit. The trick was to use Currentpage and reference doc type in plural (i.e. doc type name + "s"). Thanks once again for all suggestions!

    // Best regards Martin.

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
            @{
    
                var homePage = CurrentPage.AncestorsOrSelf(1).First();
                var newsOverview = homePage.NyheterMains.First();
                var newsItems = newsOverview.EnskildNyhets.OrderBy("publishDate desc, createDate desc").Take(3); }
    
                 <ul class="nyhetslista">
                @foreach (var newsItem in newsItems)
                {
                    // If the editor has not explicitly provided the "Page title" property page 
                    // then just show the name of the page otherwise show the provided title
                    var title = string.IsNullOrWhiteSpace(newsItem.Title) 
                        ? newsItem.Name
                        : newsItem.Title;
    
                    // If the editor has not explicitly set the publishDate property then show the create date
                    var dateTime = newsItem.PublishDate == default(DateTime) 
                        ? newsItem.CreateDate
                        : newsItem.PublishDate;
    
                    <li>
                        <article class="">
                            <h3><a href="@newsItem.Url">@newsItem.huvudrubrik</a></h3>
                    <ul>        
    <li>@newsItem.nyhetsdatum.ToString("d MMM yyyy")</li>       
    <li>@newsItem.huvudrubrik</li> 
    <li>@newsItem.underrubrik</li>
                    <hr class="line_gradient" />        <br>        </ul>
                        </article>
                    </li>
                } </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft