Retrieve Model.Title from a different page

Topics: Customizing Orchard, General, Writing modules
Oct 8, 2011 at 9:02 PM

I have a breadcrumb template that we're using throughout the site, and basically, if I'm on /controlpanel/training, I need to be able to get the model title for /controlpanel to use in the breadcrumb. How would I go about doing this?

Oct 8, 2011 at 9:29 PM

If the title is the one on the Route part, something like Model.ContentItem.RoutePart.Title should do the trick.

Oct 8, 2011 at 9:59 PM

I'm not quite sure I follow...

Oct 8, 2011 at 10:14 PM

mmh., not sure I follow what you don't follow: you asked a question, and I gave you code that seems to answer that question. So unless I've misunderstood the question...

Oct 8, 2011 at 10:21 PM

Model.ContentItem.RoutePart.Title gets me the page title for the current page, no problem, but I need the title from a different page. In the example above, the breadcrumb would be "Home > (/controlpanel title) > (/controlpanel/training title).

Oct 8, 2011 at 10:23 PM

I see. You need a reference to these other content items. Once you have that, you can put it inot a dynamic variable and apply the same path as above.

Oct 8, 2011 at 11:43 PM

"if it has a Routable", but the correct way to do it is to call contentManager.GetItemMetadata(contentItem).DisplayText, which will get the Routable Title if there is one, or another valid title for this content item.

Oct 8, 2011 at 11:48 PM

Ha! thanks Sébastien :)

Oct 10, 2011 at 2:15 PM

And how would I define contentItem as a different page?

Oct 10, 2011 at 7:30 PM
Edited Oct 10, 2011 at 7:31 PM

I'm sorry, I don't quite understand the question. You mean you don't have the reference to that other content item? What relates it to the current item?

Oct 10, 2011 at 8:11 PM

The slug. /contentupdates and /contentupdates/page are two completely different pages, but in a "hierarchy", so the breadcrumb on /contentupdates/page needs to know the proper page title from /contentupdates. Sorry if I'm making this difficult; I still don't have a very good grasp of MVC or Orchard's models... used to building everything from scratch.

Oct 10, 2011 at 8:14 PM

Oh, ok, I see. Well, you'd have to split the slug along the slashes then, and query the content manager for items with the parent slugs.

Oct 10, 2011 at 8:51 PM

I gathered that much, and I'm already splitting out the slug just fine. It's the querying part that I don't know.

Oct 10, 2011 at 9:14 PM

Something like

contentManager.Query<RoutePart, RoutePartRecord>(VersionOptions.Published).Where(r => r.Path == matchedPath)


Oct 11, 2011 at 4:58 PM

What namespace does contentManager need?

Oct 11, 2011 at 6:12 PM

With a

@using Orchard.ContentManagement

You can get a reference to the content manager by doing

Oct 11, 2011 at 6:25 PM

Hmm... neither VS or Orchard are seeing the contentManager object as available... The name 'contentManager' does not exist in the current context.

Oct 11, 2011 at 6:31 PM

And it doesn't complain bout the using statement? You did assign what the Resolve method returned, right?

Oct 11, 2011 at 6:48 PM

Ah, gotcha. Sorry, I'm doing most of my editing over FTP, and generally don't have Intellisense immediately at my disposal. Now it's complaining about the RoutePart and RoutePartRecord, and not giving any suggestions on the namespace...

Oct 11, 2011 at 6:55 PM

Let me search that for you ;) You should get the source code...


Oct 12, 2011 at 4:33 PM

My searching on the subject of Orchard hasn't been particularly helpful... I can't seem to find any real documentation on it. Apologies if I sound like a noob, but this is a relatively large project being crammed into three weeks, while I'm busy fixing everything else that happens to break. This is the only outstanding issue left, and between my inability to find documentation and my lack of experience with MVC, something just isn't clicking. So I have the following:

		var contentManager = WorkContext.Resolve<IContentManager>();		
		IContentQuery rec = contentManager.Query<RoutePart, RoutePartRecord>(VersionOptions.Published).Where(r => r.Path == "/" + category);

and I'm getting "best overloaded method match has some invalid arguments". What's missing?

Oct 12, 2011 at 6:45 PM

On the second line? I don't know, it compiles fine here. Note that you can (and probably should) use a var on the second line as well.

Oct 12, 2011 at 6:54 PM

Ok, so there was a stupid moment on my part there... scope issues. Anyway, now that i have var rec, and it's compiling, what the hell *is* rec at this point, and how do I get a title out of it?

Oct 12, 2011 at 6:59 PM

Rec at this point is still a query object. You need to get its first result (if there is one, but there should be), and that will be the route part object.

Oct 12, 2011 at 7:16 PM

Holy crap that took too long to figure out... thanks a ton!