Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is current action a ChildAction?

Tags:

How can I determine if current action is a ChildAction or routed main action? Should I check the URL and compare to the action's name? That's not so nice, since it's dependent on routing patterns...
Or should I make two actions of the same name, put a ChildActionOnly on one of them and have separate logic (mainly returning View() or PartialView())? How will the overloads be differentiated?

Okay, from an other perspective: How to make it so, that if it's a ChildAction then return a PartialView, otherwise a full View?

like image 963
TDaver Avatar asked Feb 05 '11 11:02

TDaver


People also ask

What is a child action in MVC?

What is an MVC Child Action. A Child Action in ASP.NET MVC is kind of similar to that of a User Control in ASP.NET web forms. It allows for a controller to execute for a portion of the rendered area of a view, like in Web Forms where you can execute a UserControl for a portion of the rendered area of a page.

What is child action?

Basically a child action is a controller action that you could invoke from the view using the Html.Action helper: @Html.Action("SomeActionName", "SomeController") This action will then execute and render its output at the specified location in the view.


1 Answers

You could use the IsChildAction property:

public ActionResult Index() {     if (ControllerContext.IsChildAction)     {          // The Index action was invoked as child action using           // @Html.Action("index")     }     ... } 
like image 150
Darin Dimitrov Avatar answered Oct 29 '22 16:10

Darin Dimitrov