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?
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
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);
}
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
Hi David,
You have 2 ways to get Speaker values in a controller.
Use ContentService - https://our.umbraco.org/documentation/reference/management/services/contentservice, get data from the database, always data is actual but it's slow
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
Hi David,
Share with us - did you solve the issue?
Thanks,
Alex
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?
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
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.
is working on a reply...