Copied to clipboard

Flag this post as spam?

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


  • David Gregory 85 posts 211 karma points
    Nov 08, 2016 @ 14:13
    David Gregory
    0

    Content Picker in Controller

    Hi

    I have a doc type called Talk which has properties such as TalkTitle, TalkDate, TalkDescription etc. Seperate to this I have a doc type called Speaker which has properties like SpeakerName, SpeakerBio etc.

    In the Talk doc type I have a content picker property that allows users to associacte a Speaker with a Talk.

    In a template to list all the Talks I can select all the Talks and foreach through them and display e.g @item.TalkTitle and for the Speaker values I have used @Umbraco.Content(item.Speaker).SpeakerName and this seems to work for me.

    My question is though, how do I get the Speaker values in a controller? In my index method I have the RenderModel passed in so I can get the Talk properties simply by using model.TalkTitle, but how do I dig down into the Speaker properties of the Talk model?

    Hope this makes sense. Thanks using 7.5.4

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 08, 2016 @ 16:34
    Alex Skrypnyk
    0

    Hi David,

    You have 2 ways to get Speaker values in a controller.

    1. Use ContentService - https://our.umbraco.org/documentation/reference/management/services/contentservice, get data from the database, always data is actual but it's slow

    2. Use UmbracoHelper - get data from XML cache, faster than contentService but it's cache, so it's good only for getting data, changing data you have to do cia ContentService

    Hope it will help you.

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 15:17
    Alex Skrypnyk
    0

    Hi David,

    Share with us - did you solve the issue?

    Thanks,

    Alex

  • David Gregory 85 posts 211 karma points
    Nov 15, 2016 @ 16:19
    David Gregory
    0

    Hi, thanks for the reply.

    Yes and no really. I have got something to do what I wanted but I am not happy with the way I got there. Can you see any problems with what I hacked around with below?

    public ActionResult Index(string s = "", string t = "")
        {
            List<TalkViewModel> talks = new List<TalkViewModel>();
            var selection = CurrentPage.Site().FirstChild().Children("talk").Where("Visible");
    
            foreach (var item in selection)
            {
                Talk talk = Umbraco.TypedContent(item.Id) as Talk;
                Speaker speaker = Umbraco.TypedContent(talk.Speaker) as Speaker;
                talks.Add(new TalkViewModel
                {
                    TalkTitle = talk.TalkTitle,
                    TalkSpeaker = speaker.FirstName
                });
            }
    
            if (!String.IsNullOrEmpty(s))
            {
                talks = talks.Where(m => m.TalkSpeaker == s).ToList();
            }
            if (!String.IsNullOrEmpty(t))
            {
                talks = talks.Where(m => m.TalkTitle.Contains(t)).ToList();
            }
    
            return View("Index",talks);
        }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 16:32
    Alex Skrypnyk
    0

    Hi David,

    I don't see any reasons to write this code inside controllers, maybe we can write it in view?

    Or do you use RenderMvcController for filling stringly typed results to model?

    Thanks,

    Alex

  • David Gregory 85 posts 211 karma points
    Nov 15, 2016 @ 16:34
    David Gregory
    0

    I did have it all in a view which was a bit simpler but I wanted to move it out to the controller. yes I am using RenderMvcController.

Please Sign in or register to post replies

Write your reply to:

Draft