9

I have a partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DomainModel.Entities.Product>" %>

<div class="item">
    <h3><%= Model.Name %></h3>
    <%= Model.Description %>

    <% using (Html.BeginForm("AddToCart", "Cart")) { %>
        <%= Html.Hidden("ProductID") %>
        <%= Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery) %>
        <input type="submit" value="+ Add to cart" />
    <% } %>

    <h4><%= Model.Price.ToString("c")%></h4>
</div>

And here is the html that gets rendered:

<div class="item"> 
    <h3>Kayak</h3> 
    A boat for one person
    <form action="" method="post">
        <input id="ProductID" name="ProductID" type="hidden" value="1" /> 
        <input id="returnUrl" name="returnUrl" type="hidden" value="/" /> 
        <input type="submit" value="+ Add to cart" /> 
    </form> 
    <h4>$275.00</h4> 
</div> 

Nothing happens when the submit button is clicked and I am pretty sure it's because the form action attribute has no value. Shouldn't BeginForm(action, controller) take care of rendering out the form action? What am I doing wrong?

EDIT

Code from CartController AddToCart action:

    public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
    {
        Product product = productsRepository.Products.FirstOrDefault(p => p.ProductID == productID);

        cart.AddItem(product, 1);
        return RedirectToAction("Index", new { returnUrl });
    }

EDIT 2

The view that renders the partial:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% foreach (var product in Model) { %>
        <% Html.RenderPartial("ProductSummary", product); %>
    <% } %>

    <div class="pager">
    Page:
    <%=Html.PageLinks((int)ViewData["CurrentPage"],
                      (int)ViewData["TotalPages"],
                      x => Url.Action("List", new { page = x, category = ViewData["CurrentCategory"] })) %>
    </div>
</asp:Content>

EDIT 3

Routes:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            null, // don't need a name
            "", // matches the root URL, i.e. ~/
            new { controller = "Products", action = "List", category = (string)null, page = 1 } //Defaults
        );

        routes.MapRoute(
            null, // don't need name
            "Page{page}", // URL pattern, e.g. ~/Page683
            new { controller = "Products", action = "List", category = (string)null }, // defaults
            new { page = @"\d+" } // constraints: page must be numerical
        );

        routes.MapRoute(null,
            "{category}",
            new { controller = "Products", action = "List", page = 1 });

        routes.MapRoute(null,
            "{category}/Page{page}",
            new { controller = "Products", action = "List" },
            new { page = @"\d+" } // constraints: page must be numerical
        );

    }
3
  • Can you post the code from CartController.AddToCart() action ? Commented Jun 16, 2009 at 18:18
  • What does you Html.RenderPartial code look like in the View? Commented Jun 16, 2009 at 18:48
  • 1
    What do your routes look like? Commented Jun 16, 2009 at 19:04

2 Answers 2

15

It looks like you don't have a default route set up. BeginForm uses UrlHelper.GenerateUrl to match up the action/controller names to your route collection. So if you don't have a route that maps to AddToCart, then it can't generate a URL for it. Try adding this to the bottom of your routes:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Products", action = "List", id = "" }
);
Sign up to request clarification or add additional context in comments.

2 Comments

Upvote. Thanks so much. I'm surprised there aren't more mentions of this in The Google. Also, I kind of wish that if no route were matched that Html.BeginForm(...) would throw an exception.
@ardave, I totally agree: it should fail fast, not when I submit the form and wonder why it’s not working!
0

This is from the main application example used in Steven Sanderson's excellent 'Pro ASP MVC Framework' book.

Funnily enough I made exactly the same mistake and omitted the final .MapRoute call given in the listing on page 130.

routes.MapRoute("Default", "controller}/{action}"

It was Johnny G's answer to this post that helped me to find my mistake as well.

Nice one Johnny!

1 Comment

But, to be fair, he couldn't format a comment as cleanly as this answer, so, whilst it's wrong, I can understand it THIS TIME?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.