Surface Controller and Child Action - remember our old good User Controls in ASP.NET

In this tutorial i will show you how you can create a Child Action using the Surface Controller which you can reuse everywhere in your code.

Furthermore we will pass a paramter to this Controller. 

As an Example we will build an Contact Form to which we will pass a the ID of our object as paramter.


What is a 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, just like in Web Forms where you can execute a UserControl for a portion of the rendered area of a page"


Creating a Child Action

Create a Controller under the "Controllers" Folder. Please note to name your controller with the "SurfaceController"- Sufix. In my example i called it "ContactFormSurfaceController". Change the inheritance to "SurfaceController".


public class ContactFormSurfaceController : SurfaceController
{ public ActionResult Index() { } }

To tell the controller that the Index function is a Child Action you just decorate the function with ethe [ChildActionOnly] attribute



Caglar Tasci

Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defence against complexity. — David Gelernter

comments powered by Disqus