Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect View or PartialView in controller

Tags:

asp.net-mvc

In a controller can I detect if I'm "controlling" a View or a partial view?

I have a shared controller that sits between any controller and Controller, there are things I would only like to happen if its a View rather than a partial view.

Ideally in protected override ViewResult View(...

like image 909
NikolaiDante Avatar asked Jul 02 '09 12:07

NikolaiDante


People also ask

Can we pass data from view to controller?

There are four ways to pass the data from View to Controller which are explained below: Traditional Approach: In this approach, we can use the request object of the HttpRequestBase class. This object contains the input field name and values as name-value pairs in case of the form submit.

How do you check if a view is partial or not?

Solution 1 There is no difference in the markup between a partial view and a view; the only difference is in how they are delivered to the browser. A partial view is sent via AJAX and must be consumed by JavaScript on the client side, while a View is a full postback. That's it.

How does a controller find a view in MVC?

MVC by default will find a View that matches the name of the Controller Action itself (it actually searches both the Views and Shared folders to find a cooresponding View that matches). Additionally, Index is always the "default" Controller Action (and View).


2 Answers

Check

ControllerContext.IsChildAction

in your action method, or

filterContext.IsChildAction

in OnActionExecuted

like image 131
Martin Meixger Avatar answered Oct 21 '22 22:10

Martin Meixger


Have a different route for each. /home/about and /home/about/partial that both are routed to the same controller method. Basically, rather than trying to sniff around for things, EXPRESS it explicitly, through a route, or parameter.

like image 44
Scott Hanselman Avatar answered Oct 21 '22 20:10

Scott Hanselman