Mahdi Taghizadeh
Mahdi Taghizadeh

Reputation: 903

Html.RenderAction causes "No route in the route table matches the supplied values"

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.

Upvotes: 17

Views: 12334

Answers (7)

RickWeb
RickWeb

Reputation: 2135

I have had this issue before, it was where the route didn't include the controller.

            context.MapRoute(
            "Route_default",
            "Stuff/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
        );

I still wanted to be able to call an action straight after the Area so i added the following route like so.

            context.MapRoute(
            "Route_default",
            "Search/{action}/{id}",
        new { controller = "Search", action = "Index", id = UrlParameter.Optional }
        );

        context.MapRoute(
            "Route_Controller",
            "Stuff/{controller}/{action}/{id}",
            new { controller = "Something", action = "Index", id = UrlParameter.Optional }
        );

Upvotes: 0

Memet Olsen
Memet Olsen

Reputation: 4608

Adding a third parameter like this was the solution for me (in razor):

@{Html.RenderAction("ActionName", "ControllerName", new { area = string.Empty });}

Upvotes: 14

Jorrit Salverda
Jorrit Salverda

Reputation: 765

I had the same error. It was caused by altering the default route; apparently it explicitly searches for a route name "Default".

Upvotes: 6

Sam Saffron
Sam Saffron

Reputation: 131192

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.

Upvotes: 11

thitemple
thitemple

Reputation: 6059

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.

Upvotes: 0

Pieter Germishuys
Pieter Germishuys

Reputation: 4886

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

Upvotes: 6

Gopher
Gopher

Reputation: 927

Have you registered any additional routes for you application?

Upvotes: 0

Related Questions