views:

459

answers:

4

I'm trying to use Html.RenderAction in ASP.NET MVC 2 RC2 in this way:

In Menu Controler:

[ChildActionOnly]
public ActionResult ContentPageMenus()
{
     var menus = _contentPageMenuRepository.GetAll().WithCulture(CurrentCulture);
     return PartialView(menus);
}

And in my Index view (for Index action of Home controller):

 <% Html.RenderAction("ContentPageMenus", "ContentPageMenu");%>

But I always get this error message: No route in the route table matches the supplied values.

A: 

Have you registered any additional routes for you application?

Gopher
I found the problem. I always remove {controller}/{action} route and customize all my routes with lowercase REST like URLs. But for Html.RenderAction it is necessary to have such general route. I added that general route to the end of my routes list and it worked.
Mahdi
+1  A: 

What is your controller's name? By default the following is what happens with your routes.

The Controller name specified in your RenderAction method is searched for with "Controller" appended to that name.

The Action method in that Controller gets called and a View returned.

So, by looking at your code, the following will happen

  1. You should have a Controller called "ContentPageMenuController"
  2. You should have an Action called "ContentPageMenus" which you have
  3. You should have a view called ContentPageMenus()

This is assuming that you haven't changed the defaulting routing and haven't added new ones that will affect your routing

PieterG
I found the problem. I always remove {controller}/{action} route and customize all my routes with lowercase REST like URLs. But for Html.RenderAction it is necessary to have such general route. I added that general route to the end of my routes list and it worked.
Mahdi
A: 

Why don't you try using the strong typed method?

Try this:

<% Html.RenderAction<ContentPageMenusController>(x => x.ContentPageMenus()); %>

You have to fill the exactly name of the class.

VinTem
Is it available in ASP.NET MVC 2 RC2? I can't see this.
Mahdi
yes it is... I took this code from a project using it
VinTem
It appears that this method is actually from MVC Futures: http://forums.asp.net/p/1565646/3901280.aspx
StriplingWarrior
A: 

MVC Futures used to allow rendering of actions that had no routes. This has changed in ASP.NET MVC2.

If you want RenderAction to work and would like to hide your route so its not publicly accessible.

  1. Add a route for your action in globals.asax.cs.
  2. Decorate your action with the [ChildActionOnly] attribute.
Sam Saffron