ASP.NET MVC Html.BeginForm Problem

江枫思渺然 提交于 2019-12-05 02:13:16

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!