Thomas Stock
Thomas Stock

Reputation: 11256

RenderAction in _layout gives "No route in the route table matches the supplied values."

On my _layout.cshtml I have the following line

Html.RenderAction("ChamberComment", "Carnets", new { Area = "Chamber"});

This gives the following error:

No route in the route table matches the supplied values.

I am using routedebugger, so under the page that displays this error, I can actually see the matching route being found:

enter image description here

I have this route defined using AttributeRouting:

namespace MyProject.Net.Site.Areas.Chamber.Controllers
{
    [RouteArea("Chamber", AreaPrefix = "")]
    [AuthorizeRoles(Roles = RoleDiscriminator.Chamber)]
    public class CarnetsController : BaseController
    {
        [HttpGet]
        [Route("chamber/carnets/{slug}/{step}/chamberRemarks")]
        public ActionResult ChamberComment(string slug, string step)
        {

I can also go straight to the URL

http://myproject/chamber/carnets/18-reffr/general/chamberRemarks

and it renders the partial in the browser, with underneath the expected RouteDebugger information...

So somehow the system is unable to translate the RenderAction statement to the correct action when used in the _layout masterpage, but it is able to route the specified url to this action..

Any idea in which direction I should be looking?

Upvotes: 2

Views: 1271

Answers (2)

Todd
Todd

Reputation: 13173

A related problem occurs if you have the sub action in a top-level controller and you try to render it from within a layout. In my case, this occurred in a project that had areas. I'm not sure if it also occurs in projects without areas.

I have a default route which should satisfy the [Render]Action parameters. However, this case was always failing:

Html.Action("ActionName", "ControllerName")

The solution was to add a null area:

Html.Action("ActionName", "ControllerName", new { area = "" })

Now, the exception no longer occurs and the partial view is rendered.

Upvotes: 2

Daniel J.G.
Daniel J.G.

Reputation: 34992

Have you tried providing the slug and step parameters as in:

 Html.RenderAction("ChamberComment", "Carnets", new { Area = "Chamber", slug = "18-reffr", step = "general"});

If those parameters are not in the current Url being rendered, you will need to manually pass them to the Html.RenderAction method.

As per your comment, the slug is found in the current Url but step is hardcoded so you will need to provide it to the helper.

Upvotes: 1

Related Questions