ASP.NET MVC Html.BeginForm问题 [英] ASP.NET MVC Html.BeginForm Problem
问题描述
我有一个局部视图:
<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl< DomainModel.Entities.Product>中%GT;< DIV CLASS =项>
&所述; H3>&下;%= Model.Name%GT;&下; / H3>
&所述;%= Model.Description%GT; <使用(Html.BeginForm(AddToCart,车)){%GT%;
&所述;%= Html.Hidden(产品ID)%>
&所述;%= Html.Hidden(RETURNURL,ViewContext.HttpContext.Request.Url.PathAndQuery)%GT;
<输入类型=提交值=+添加到购物车/>
<%}%GT; &所述; H4>&下;%= Model.Price.ToString(C)%>&下; / H4>
< / DIV>
这是被呈现的HTML:
< DIV CLASS =项>
< H3>皮划艇< / H3 GT&;
一个单人艇
<形式的行动=方法=后>
<输入ID =产品名称=产品类型=隐藏值=1/>
<输入ID =RETURNURLNAME =RETURNURL类型=隐藏值=//>
<输入类型=提交值=+添加到购物车/>
< /表及GT;
< H4> $ 275.00< / H4>
< / DIV>
点击提交按钮时,没有任何反应,我是pretty肯定是因为表单action属性没有价值。不应该BeginForm(动作控制器)照顾呈现出形式的行动?我在做什么错了?
修改
从CartController AddToCart行动code:
公共RedirectToRouteResult AddToCart(车车,诠释的productID,串RETURNURL)
{
产品产品= productsRepository.Products.FirstOrDefault(P => p.ProductID ==的productID); cart.AddItem(产品,1);
返回RedirectToAction(指数,新的{RETURNURL});
}
编辑2
这使得局部视图:
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
<%的foreach(在型号VAR产品){%GT;
&所述;%Html.RenderPartial(ProductSummary产品); %GT;
<%}%GT; < DIV CLASS =寻呼机>
页:
<%= Html.PageLinks((int)的计算机[当前页],
(INT)计算机[总页数],
X => Url.Action(列表,新的{页= X,类别=计算机[CurrentCategory]}))%GT;
< / DIV>
< / ASP:内容>
编辑3
路线:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
空,//不需要名
,//匹配根URL,即〜/
新{控制器=产品,行动=名单,类别=(字符串)空,页面= 1} //默认
); routes.MapRoute(
空,//不需要名字
第{PAGE},// URL模式,例如〜/ Page683
新{控制器=产品,行动=名单,类别=(字符串)空} //默认
新{页= @\\ D +} //约束:页面必须是数字
); routes.MapRoute(NULL,
{类别},
新{控制器=产品,行动=名单,页面= 1}); routes.MapRoute(NULL,
{}类/页{PAGE},
新{控制器=产品,行动=名单},
新{页= @\\ D +} //约束:页面必须是数字
); }
看起来你不是有一个默认的路由设置做。 BeginForm
使用 UrlHelper.GenerateUrl
来匹配动作/控制器名称为您的路由集合。所以,如果你没有一个映射到 AddToCart
路线,那么就无法生成它的URL。尝试添加这对你的路由的底部:
routes.MapRoute(
默认,
{控制器} / {行动} / {ID}
新{控制器=产品,行动=名单,ID =}
);
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
);
}
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 = "" }
);
这篇关于ASP.NET MVC Html.BeginForm问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!