Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 445 posts 1059 karma points
    Apr 20, 2017 @ 13:02
    Ayo Adesina
    0

    Get Recent Posts From inside MVC controller

    Hi Guys,

    I'm trying to do something that is fairly simple, but as I have never used Articulate before I'm struggling.

    I want to get the latest blog post, this is what I have got so far.

        public ActionResult HomePage(RenderModel model)
        { 
        var articulateMasterModel = model.Content as IMasterModel;
                homePageViewModel.LastestBlogPost = helper.GetRecentPosts(articulateMasterModel, 1).OrderByDescending(x => x.PublishedDate).First();
    
        return View(homePageViewModel);
        }
    

    articulateMasterModel ends up being null - what is the correct way to initialize it?

  • David Peck 690 posts 1896 karma points c-trib
    Apr 20, 2017 @ 13:32
    David Peck
    0

    Does that mean that articulateMasterModel isn't a IMasterModel but something else.

    A RenderModel should always have a non-null Content property so perhaps just go with var articulateMasterModel = (IMasterModel)model.Content;. You'll at least see why the cast is invalid that way.

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Apr 20, 2017 @ 23:32
    Shannon Deminick
    1

    You'll need to explain more about what this controller you have written is, what is it?

    I assume this page is not an articulate page, in that case model.Content is just your normal IPublishedContent for the page rendering.

    If you want to lookup an IMasterModel for Articulate, you need to go lookup the Articulate root node - using normal Umbraco content queries. Once you have that you can new-up an IMasterModel instance such as the generic MasterModel https://github.com/Shazwazza/Articulate/blob/master/src/Articulate/Models/MasterModel.cs , i.e. new MasterModel(articulateRootNode);

  • Ayo Adesina 445 posts 1059 karma points
    Nov 25, 2017 @ 23:11
    Ayo Adesina
    0

    Thanks, Shannon Exactly what I was looking for!

Please Sign in or register to post replies

Write your reply to:

Draft