views:

296

answers:

1

In asp.net mvc, you can easily call/add a controller from a view, but what is the easiest way to add child/subcontrollers from a controller. What Im getting at is I want to dynamically build a list of child controllers from within the controller itself, not from the view.

The pattern I have in mind is derived from the old ibuyspy portal/dnn where you come up with a list of pluggable modules that you want to inject into the page. Each module is, itself, a controller, and is ignorant that it is a child request. But, dnn uses the ui/views to inject the modules, as where I want to create a list of modules in the controller, then tell the view to inject them.

Thanks in advance,

Jesse

A: 

If you have an array of objects in your view model that contains information corresponding to the desired modules, you can inject them into the view using RenderAction like this:

<div id="LeftColumn">
    <% foreach (module in Model.Modules) { 
        Html.RenderAction(module.ActionName, module.ControllerName, new {id = module.id}); 
    } %>
</div>

RenderAction is a method that calls a method on a controller, and injects the result into the page at the location where RenderAction is called. It is part of the ASP.NET MVC Futures assembly.

Robert Harvey
Ah, that makes sense. I guess it was so simple, I was trying to complicate the issue. Thanks for setting me right.
jf26028
Is there an alternative if we are not using MVC Future Assembly?
TimLeung
You could try Subcontrollers or RenderPartial. But RenderAction is going to be included in MVC framework 2.0 anyway. See also http://stackoverflow.com/questions/1214052/should-i-use-areas-or-renderaction-in-asp-net-mvc/1214077
Robert Harvey