简体   繁体   中英

Determine if View is being rendered as Partial

Is there a way to determine if a view is rendering as a partial?

I'm hoping to extend the reuse of a partial that I'm writing by catching this...and if necessary assigning the appropriate layout to the View.

At the moment I'm just rendering it in a div, but I could also see us using it as a modal and possible it's own page.

(the modal shouldn't require any change so no worries there)

EDIT: To clear up what I'm asking.

I'm wondering if there is anyway to determine the difference between a view being rendered by...

/path/to/controller

and

Html.Partial("/path/to/view.cshtml")

Why not @if (Layout==null)? Still I would recommend another view for the "own" page and set the layout there.

In your view (assuming Razor syntax):

@if(typeof(this) == Controller.PartialView)) //code

or

@if(this is Controller.PartialView) //code

Based on @Pheonixblade9 's response and the lack of other answers it doesn't appear like this is possible at the moment. I ended up just binding the Model of the View as bool and pass this value in when rendering the view/partial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM