C# ASP.NET MVC Html.BeginForm 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1003040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Html.BeginForm Problem
提问by Ronnie Overby
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:
这是被渲染的 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>5.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?
单击提交按钮时没有任何反应,我很确定这是因为表单操作属性没有值。BeginForm(action, controller) 不应该负责渲染表单动作吗?我究竟做错了什么?
EDIT
编辑
Code from CartController AddToCart action:
来自 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 });
}
EDIT 2
编辑 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
编辑 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
);
}
采纳答案by John G
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:
您似乎没有设置默认路由。BeginForm
用于UrlHelper.GenerateUrl
将动作/控制器名称与您的路线集合相匹配。因此,如果您没有映射到 的路由AddToCart
,则无法为其生成 URL。尝试将其添加到路线的底部:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Products", action = "List", id = "" }
);
回答by Graham Watson
This is from the main application example used in Steven Sanderson's excellent 'Pro ASP MVC Framework' book.
这是来自 Steven Sanderson 出色的“Pro ASP MVC 框架”一书中使用的主要应用程序示例。
Funnily enough I made exactly the same mistake and omitted the final .MapRoute call given in the listing on page 130.
有趣的是,我犯了完全相同的错误,并省略了第 130 页清单中给出的最后一个 .MapRoute 调用。
routes.MapRoute("Default", "controller}/{action}"
It was Johnny G's answer to this post that helped me to find my mistake as well.
约翰尼 G 对这篇文章的回答也帮助我找到了我的错误。
Nice one Johnny!
不错的约翰尼!