Conditional [Themed] attribute

Topics: Customizing Orchard, General
Feb 23, 2011 at 6:01 PM

Hi,

Related to a question I'd asked earlier, I'd like to return an ActionResult where the [Themed] attribute is set to false on the condition that a certain querystring is set, for example. This is proving to be difficult, is there any way I can do what I'm looking to do?

Essentially I'm trying to get the following functionality:

        [Themed]
        public ActionResult Index()
        {
            if (Request.QueryString["json"] != null && Request.QueryString["json"].ToLower() == "true")
            {
                Themed = false;
            }
            else
            {
                Themed = true;
            }
            return View("HelloWorld");
        }

Is this kind of thing possible?

Thanks very much :)

Coordinator
Feb 23, 2011 at 6:45 PM

It might be possible, but I haven't researched it. The question that I want to ask first is why don't you have two separate actions?

Feb 23, 2011 at 10:40 PM

I've been looking around for conditional theming, it doesn't seem to be possible but I hoped you might know.

The reason is simply in to keep things clean for AJAX. Say I have a link which will go to the "next" piece of content on the page - it links to /controller/action, so I thought I could just, when the javascript click event-handled function is called, I append a "?json=true" to the url and cancel the click event, then get the contents of the page WITHOUT the theme from an XMLHttpRequest/jQuery. This will work though, I simply need to have an "Index" and an "IndexJson" or something.

Coordinator
Feb 24, 2011 at 12:21 AM

Well, if you add that json parameter to your routing, it should be super easy to route the two requests to different actions. And it would get rid of that ugly if/else.

Coordinator
Feb 24, 2011 at 1:36 AM

Try Json(“HelloWorld”) instead for the client request (in part to get around getting themed) and use Request.IsAjaxRequest() to know when you should do that.

Coordinator
Feb 24, 2011 at 1:37 AM

(ah, yes, well done, totally forgot that very simple thing. Thanks Nathan)

Coordinator
Feb 24, 2011 at 1:42 AM

No worries. It would help if we actually took advantage of that ourselves ;)

Feb 24, 2011 at 10:46 AM

Thanks for the suggestions, guys.

It almost works... but Json("HelloWorld") only actually returns the string "HelloWorld". Somehow I need to return the view without the theme. Any more ideas, or should I go with the original idea?

Thanks!

Feb 24, 2011 at 11:38 AM

Ok I've got it! Thanks for your help guys!

return PartialView("HelloWorld");

This seems to work.