动作不触发
这是我的控制器签名:
public class CartController : Controller
{
public ActionResult Index()
{
CartViewModel cartViewModel = _cartRepository.GetCart(SessionVariables.CartId);
return View(cartViewModel);
}
public ActionResult List()
{
return View();
}
[Authorize]
public ActionResult AddToCart(int productId, int quantity = 1)
{
return RedirectToAction("Index");
}
[HttpPost]
[ActionName("AddToCart")]
public ActionResult AddToCartFromDetails(int productId = 0, int quantity = 1)
{
return RedirectToAction("AddToCart", new {productId , quantity});
}
}
这是我在 Global.asax 上的路线:
routes.MapRouteLowerCase(
"AddToCart", // Route name
"products/addtocart/{productId}", // URL with parameters
new { controller = "Cart", action = "AddToCart", productId = UrlParameter.Optional } // Parameter defaults
);
这是我的表单,它不会转到我的 CartController 上的 [HttpPost] 操作:
@using (Html.BeginForm("AddToCart", "Cart", null, FormMethod.Post, null))
{
<div class="quantity-container">
<div>
Quantity :</div>
<input type="text" name="quantity" value="1" />
<input type="hidden" name="productId" value="@Model.ItemDetails.ItemMasterId" />
</div>
<div class="buy-btn-container">
<input type="image" src="@Url.Content("~/Content/Images/buynow_btn.png")" alt="Buy"/>
</div>
<div class="clear">
</div>
}
表单的 url 确实解析为上面设置的路线:
/产品/添加到购物车
Here is my controller signature:
public class CartController : Controller
{
public ActionResult Index()
{
CartViewModel cartViewModel = _cartRepository.GetCart(SessionVariables.CartId);
return View(cartViewModel);
}
public ActionResult List()
{
return View();
}
[Authorize]
public ActionResult AddToCart(int productId, int quantity = 1)
{
return RedirectToAction("Index");
}
[HttpPost]
[ActionName("AddToCart")]
public ActionResult AddToCartFromDetails(int productId = 0, int quantity = 1)
{
return RedirectToAction("AddToCart", new {productId , quantity});
}
}
Here is my route on Global.asax:
routes.MapRouteLowerCase(
"AddToCart", // Route name
"products/addtocart/{productId}", // URL with parameters
new { controller = "Cart", action = "AddToCart", productId = UrlParameter.Optional } // Parameter defaults
);
Here is my form which does not go to the [HttpPost] action on my CartController:
@using (Html.BeginForm("AddToCart", "Cart", null, FormMethod.Post, null))
{
<div class="quantity-container">
<div>
Quantity :</div>
<input type="text" name="quantity" value="1" />
<input type="hidden" name="productId" value="@Model.ItemDetails.ItemMasterId" />
</div>
<div class="buy-btn-container">
<input type="image" src="@Url.Content("~/Content/Images/buynow_btn.png")" alt="Buy"/>
</div>
<div class="clear">
</div>
}
The url for the Form does resolve to my route set above:
/products/addtocart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的操作名称“添加到购物车”使用相同的参数(两个整数,并且都是可选的启动)使用了两次,因此当必须路由匹配的 URL 时,MVC 无法进行区分。
Your action name "Add To Cart" is used twice with the same parameters (two ints, and both optional to boot), so MVC cannot make the distinction when a matching URL has to be routed.
将您的路由放置在
global.asax
中的默认路由上方。它既匹配默认路由又匹配您的特定路由。因此,MVC 使用它第一个找到的那个。看来 MVC 在 BeginForm 中做了一些奇怪的事情,因为您的路线适用于 GET?因此,我会改用 BeginRouteForm:
http://msdn.microsoft.com/en -us/library/dd505047.aspx
Place your route above the default route in
global.asax
. It matches both the default route and your specific one. MVC is therefore using the one it finds first.It seems like MVC does something strange in BeginForm since your route works for GET? I would therefore switch to BeginRouteForm instead:
http://msdn.microsoft.com/en-us/library/dd505047.aspx