tags:

views:

135

answers:

3

Hi,

I'm having a hack around with the MVC framework, to try some proof of concept ideas. This are not production code..

Anyhow - I have an anonymous controller. I would like to execute an Action on that controller, however, I only have this controller's action name available as a string.

How can I render the controller's action via a string name?

Thank you all!

Franko

A: 

Not sure about an anonymous controller?? But ...

If you have a look at the MVC futures project on codeplex they have a Html.RenderAction

Kindness,

Dan

Daniel Elliott
Hi Dan - Thank you for your help I was aware of this but it didnt quite fit my needs. Thank you though!
frank
A: 

You have multiple options:

  • Asp.net MVC 2 Beta 2 (used to be part of MVC Futures) that has RenderAction() built in for these purposes
  • similar thing is implemented in MVC Contrib with Sub controllers
  • or you can have PartialRequest() as explained here

But you'll have to be careful since there are issues in all of them.

Even though. Your anonymous controller is probably going to be the main obstacle. But that depends on the way you have it and how you access it. It would be easier if you'd show us some code so we could provide some more insight into your problem.

Robert Koritnik
Hi Robert - the PartialRequest() did exactly what I need - thank you!
frank
@frank: You should thank Steve then... ;)
Robert Koritnik
A: 

Although I believe that your idea may not be optimal, you can use this code:

var controller = new SomeController(null);
var controllerContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current),new RouteData(),controller);
var actionInvoker = new ControllerActionInvoker();
actionInvoker.InvokeAction(controllerContext, "Test");

I you need more details about how this code works, look at System.Web.Mvc in Reflector.

LukLed