ASP.NET MVC Html.BeginForm问题

时间:2009-06-16 18:05:00

标签: c# asp.net-mvc routing

我有一个部分观点:

<%@ 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>

以下是渲染的html:

<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> 

单击提交按钮时没有任何反应,我很确定这是因为表单操作属性没有值。 BeginForm(动作,控制器)不应该处理渲染表单动作吗?我做错了什么?

修改

来自CartController的代码AddToCart动作:

    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 });
    }

编辑2

呈现部分的视图:

<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>

编辑3

路线:

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
        );

    }

2 个答案:

答案 0 :(得分:15)

看起来您没有设置默认路线。 BeginForm使用UrlHelper.GenerateUrl将操作/控制器名称与路径集合相匹配。因此,如果您没有映射到AddToCart的路由,则无法为其生成URL。尝试将其添加到路线的底部:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Products", action = "List", id = "" }
);

答案 1 :(得分:0)

这是Steven Sanderson出色的“Pro ASP MVC框架”一书中使用的主要应用示例。

有趣的是,我犯了完全相同的错误,并省略了第130页上的清单中给出的最终.MapRoute调用。

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

Johnny G对这篇文章的回答帮助我找到了我的错误。

好一个约翰尼!