1

Does anyone know, if a partial view (PartialViewResult) in an ascx file - knows if it is running stand alone - called directly from the browser or as part of a page.

I would like to treat these cases differently.

Edit:
Case 1:
In the page case it could be that a page has a few partials that are loaded using ajax while the page is being built. The partials might be called again using ajax according to the users actions. In this case I consider them as controls on a page.

Case 2:In the stand alone case the partial could be called as part of a test directly from the browser. In this case you only see the partial part in the browser.

Case 3:In the third case the partial could be called as part of an iframe within a google chrome extension (for example). In this case you can see the partial in a page that might not have been built in your web application.

Hope this makes sense.

Thanks in advance of for those whom it concerns - happy new year.

1 Answers1

3

You could distinguish between normal requests and ajax requests (Request.IsAjaxRequest()) and child actions (ControllerContext.IsChildAction - rendered with Html.Action).

As an alternative the controller action returning the partial view could set some model property indicating that the partial was rendered via this action instead of being directly included in a view with Html.RenderPartial for example.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Darin - first of all thanks. I will update my question to clarify what I am looking for as I am not sure that I understand you completely. I will need to read a bit more about each of the alternatives you presented. –  Dec 30 '10 at 13:21
  • @Julian, here's an example `return PartialView("foo.ascx", new MyViewModel { IsPartialRendered = true });` and then inside the partial you could test the value of `Model.IsPartialRendered`. – Darin Dimitrov Dec 30 '10 at 13:23
  • @Darin - can the controller distinguish between an ajax call that is performed from within a page on site, or a direct call from the browsers navigation input? –  Dec 30 '10 at 13:40
  • @Julian, yes by using `Request.IsAjaxRequest()`. – Darin Dimitrov Dec 30 '10 at 13:41
  • @Darin - thanks again, is this Request.IsAjaxRequest() information still available in the partial or do I need to pass it down from the controller as a parameter. (Is doing so a good practice?) Please don't kick me for this one- Can an ajax call be initiated from a different site? –  Dec 30 '10 at 13:59
  • @Julian, yes it is available but I would probably add it as part of a view model property because all that a view should work with is a view model. Views should only use the information passed by the controller and not pull data themselves. As far as your second question about AJAX call from different site I don't understand what do you mean. Could you clarify? – Darin Dimitrov Dec 30 '10 at 14:01
  • @Darin Site A is my web application that works with pages and partials. Site B is someone else's site. Can site B from within its application call a partial using Ajax and get the partial back to his application? –  Dec 30 '10 at 14:09
  • @Julian, due to [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) AJAX requests cannot be sent across different domains. You could use [JSONP](http://insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html) to simulate cross domain AJAX calls. – Darin Dimitrov Dec 30 '10 at 14:11
  • @Darin - Thanks a lot I really appreciate your help. Enjoy life. –  Dec 30 '10 at 14:12
  • @Darin - Do you have any advise how can I distinguish between case 2 and 3 in my example? (If you prefer I can post this as a separate question!) –  Dec 30 '10 at 14:14
  • @Julian, no there isn't unless you send some special HTTP header along with the request. No special headers are sent with normal requests. I am not familiar with Google Chrome extensions but if you manage to send some custom header you could test for the presence of this header. – Darin Dimitrov Dec 30 '10 at 14:16