Copied to clipboard

Flag this post as spam?

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


  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 09:49
    André Lange
    0

    blog post picker - multinode tree picker - mvc

    so i am having quite a bit of difficulty to use the multinode tree picker in umbraco 7.6.4

    i am using mvc, and have controllers, models and view divided.

    I have setup my picker, and i can select blog posts in my page in backend. But how do i get the data for the picked blog posts ? so far i can only get the properties which umbraco sets, and the name of the blog post. But how do i get the data from the actual post ?

    i am using viewbag and the data needs to get pulled in a partial view.

    My Controller ( specificly TopMonthPicks, TopYearPicks )

    public class MasterController : RenderMvcController
        {
            // GET: Master
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                // set frontpage.
                var frontPage = CurrentPage.AncestorsOrSelf().FirstOrDefault(x => x.Level == 1);
    
                ViewBag.Global = new Global()
                {
                    MainMenu = GetMainMenu(frontPage),
                    Title = frontPage.GetPropertyValue<string>("pageTitle"),
                    SubTitle = frontPage.GetPropertyValue<string>("subPageTitle"),
                    CurrentPage = CurrentPage,
                    TopMonthPicks = frontPage.GetPropertyValue<IEnumerable<IPublishedContent>>("monthlyTopPicks"),
                    TopYearPicks = frontPage.GetPropertyValue<IEnumerable<IPublishedContent>>("yearlyTopPicks"),
    
                };
            }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:06
    Alex Skrypnyk
    0

    Hi Andre

    First of all, are you planning do not use Umbraco in future?

    Why did you decide to put all Umbraco data of the current page to the custom model class? You have this data in your razor code, don't need to pass it through controller one more time.

    You can get all properties of blog posts:

    var blogPosts = frontPage.GetPropertyValue<IEnumerable<IPublishedContent>>("monthlyTopPicks");
    

    Thanks,

    Alex

  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:15
    André Lange
    0

    that was me trying to get access to other data, i am not using it. I will definitely use umbraco.

    but how do i write out the values in the view ?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:30
    Alex Skrypnyk
    0

    Andre, you can render all blog posts names with this code:

        @{
            var frontPage = Umbraco.AssignedContentItem.AncestorsOrSelf().FirstOrDefault(x => x.Level == 1);
        }
    
        @foreach (var post in frontPage.GetPropertyValue<IEnumerable<IPublishedContent>>("yearlyTopPicks"))
        {
            @post.Name
        }
    
  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:38
    André Lange
    0

    i have this as an example.

    @foreach (var item in global.TopMonthPicks)
            {
            <!-- Mini Post -->
                <article class="mini-post">
                    <header>
                        <h3><a href="@item.Url">@item.Name</a></h3>
                        <time class="published" datetime="@item.CreateDate.ToString("MMMM dd, yyyy")">@item.CreateDate.ToString("MMMM dd, yyyy")</time>
                        <a href="#" class="author"><img src="Media/Static/avatar.jpg" alt="" /></a>
                    </header>
                    <a href="@item.Url" class="image"><img src="Media/Static/pic04.jpg" alt="" /></a>
                </article>
            }
    

    I can get the url for the blog post, the name and creation date. But how do i for example get the image which i have added on the post ? i have a property called "image" (single image) on the document type blogItem, which i want to get.

    How do i access this ?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:42
    Alex Skrypnyk
    0

    Andre, you need something like that:

            <!-- Mini Post -->
            <article class="mini-post">
                <header>
                    <h3><a href="@item.Url">@item.Name</a></h3>
                    <time class="published" datetime="@item.CreateDate.ToString("MMMM dd, yyyy")">@item.CreateDate.ToString("MMMM dd, yyyy")</time>
                    <a href="#" class="author"><img src="Media/Static/avatar.jpg" alt="" /></a>
    
                </header>
                @if (item.HasValue("image"))
                {
                    var mediaItem = item.GetPropertyValue<IPublishedContent>("image");
    
                    <a href="@item.Url" class="image"><img src="@mediaItem.Url" alt="" /></a>
                }
            </article>
    
  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:46
    André Lange
    0

    i get errors on hasvalue and getpropertyvalue,

    IPublishedContent does not contain a definition for getpropertyvalue.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:47
    Alex Skrypnyk
    100

    Errors because your model properties are dynamics, can you use this code:

        @{
            var frontPage = Umbraco.AssignedContentItem.AncestorsOrSelf().FirstOrDefault(x => x.Level == 1);
        }
    
    
        @foreach (var item in frontPage.GetPropertyValue<IEnumerable<IPublishedContent>>("yearlyTopPicks"))
        {
            <!-- Mini Post -->
            <article class="mini-post">
                <header>
                    <h3><a href="@item.Url">@item.Name</a></h3>
                    <time class="published" datetime="@item.CreateDate.ToString("MMMM dd, yyyy")">@item.CreateDate.ToString("MMMM dd, yyyy")</time>
                    <a href="#" class="author"><img src="Media/Static/avatar.jpg" alt="" /></a>
    
                </header>
                @if (item.HasValue("image"))
                {
                    var mediaItem = item.GetPropertyValue<IPublishedContent>("image");
    
                    <a href="@item.Url" class="image"><img src="@mediaItem.Url" alt="" /></a>
                }
            </article>
        }
    
  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:49
    André Lange
    0

    if they are dynamic, then what can i do ?

    it does not appear i can use it, since i am using my own view models.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:50
    Alex Skrypnyk
    0

    you can use strongly typed models, it's easy, just try code that I provided

  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:56
    André Lange
    0

    it does not work, since it is a partial, should i add some sort of inherit at the top or ?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 29, 2017 @ 10:56
    Alex Skrypnyk
    0

    Yes, add please:

    @inherits UmbracoTemplatePage
    
  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 10:59
    André Lange
    0

    hmmm. i get a type or namespace could not be found...

    btw. i am really appreciating this help you are giving ^^

    EDIT... found this: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

  • André Lange 108 posts 410 karma points
    Aug 29, 2017 @ 11:04
    André Lange
    0

    it results in quite a bit of errors ^^ but the same one. as you can see on the image.

    enter image description here

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Aug 30, 2017 @ 12:40
    Alex Skrypnyk
    0

    Hi Andre

    Can you paste the code as code, I would like to copy it and try to edit.

    Thanks,

    Alex

  • André Lange 108 posts 410 karma points
    Aug 31, 2017 @ 06:13
    André Lange
    0

    Its the same code you wrote to me.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Oct 02, 2017 @ 10:02
    Alex Skrypnyk
    0

    Hi Andre

    Did you solve this issue? Can you share with the community?

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft