Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC : does a partial know if it is bring requested from another page?

I have a partial view which can either be requested via an Action (Action2 in the image below), or rendered inside another page with "Html.Action()" (Action1 in the image below). From within the partial (or the partials controller) is there a way to determine which of these two methods were used to render the page?

enter image description here

like image 746
Nippysaurus Avatar asked Jul 28 '11 05:07

Nippysaurus


People also ask

What is true about partial view in MVC?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

What is advantage of partial view in MVC?

The major advantage of partial view is that we can reuse the partial view logic.

Does partial view contain the layout page?

The partial view is resides under a normal view and this view may have it's own layout page. No need of a layout page for the partial view.

What is difference between partial and render partial view?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.


2 Answers

You can use ControllerContext.IsChildAction or check DataTokens whether there is something with key "ParentActionViewContext" if you don't have access to ControllerContext.

like image 69
Lukáš Novotný Avatar answered Oct 31 '22 09:10

Lukáš Novotný


You should be able to get it from

HttpContext.Current.Request.RawUrl
like image 24
Robert Harvey Avatar answered Oct 31 '22 08:10

Robert Harvey