Cosmo D
Cosmo D

Reputation: 845

MVC2 complains that it cannot find a public action method on a controller, even though it's there

I'm using MVC2 with VS2008, and the following piece of code in the view ~/Shared/Site.Master:

<ul id="navlist">
            <li class="first"><a href="<%= Url.Content("~")%>" id="current">Home</a></li>
            <li><a href="<%= Url.Content("~/Store/")%>">Store</a></li>
            <li>
                **<% Html.RenderAction("CartSummary","ShoppingCart"); %></li>**
            <li><a href="<%= Url.Content("~/StoreManager/") %>">Admin</a></li>
        </ul>

throws the following exception:

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

which has this inner exception:

"A public action method 'CartSummary' was not found on controller 'MvcMusicStore.Controllers.ShoppingCartController'."

... that doesn't make any sense. The "CartSummary" method is defined as follows:

// GET: /ShoppingCart/CartSummary
[NHibernateActionFilter]
[ChildActionOnly]
[HttpGet]
public ActionResult CartSummary()
{
    var cart = ShoppingCart.GetCart(this.HttpContext, this.NSession);

    ViewData["CartCount"] = cart.GetCount();

    return PartialView("CartSummary");
}

So what gives? Am I missing something here?

Upvotes: 3

Views: 1309

Answers (3)

Forty-Two
Forty-Two

Reputation: 7605

Change Html.RenderAction to Html.Action or remove the [ChildActionOnly] filter

Upvotes: 0

MikeTWebb
MikeTWebb

Reputation: 9279

@Cosmo...is the name of your controller 'ShoppingCartController' or ShoppingCart. If it's 'ShoppingCartController', then the html.RenderAction will barf as the Controller name will be incorrect.

Change to: Html.RenderAction("CartSummary","ShoppingCartController");

Upvotes: 0

Peter Kiss
Peter Kiss

Reputation: 9319

What happens if you remove the [HttpGet] attribute from your child action?

If i am correct the child action will be called with the same HTTP Verb as the "main action" was called.

Upvotes: 5

Related Questions