博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc html.beginform action值不正确的问题
阅读量:4970 次
发布时间:2019-06-12

本文共 1921 字,大约阅读时间需要 6 分钟。

from: 

 

Using MVC, I have an html form helper in my view:
using (Html.BeginForm("ActionOne", "ControllerOne")) ...

Using the default route, the output for the action attribute is as expected:

 

But registrering a new route with seemingly no matches affects the output.

Routing code:

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");         routes.Add("testRoute", new Route("MyUrl", new MvcRouteHandler()));         routes.MapRoute("Default", "{controller}/{action}", new {
controller = "Home", action = "Index"}); }

Output:

 

Is this by design or am I mising something fundamental?

 

I have experienced this exact problem. I'm not sure exactly why the System.Web.Mvc.HtmlHelper seems to just use the first non-ignore route in the routetable to generate links etc from but I have found a workaround for the "BeginForm" issue.

If you have named your "Default" route in the Global.asax.cs, for example:

routes.MapRoute("Default", "{controller}/{action}", new {
controller = "Home", action = "Index" });
 

Then you can use the Html.BeginFormRoute method and call the name of the "Default" MVC route, then name the controller and action specifically, resulting in the correct url:

using (Html.BeginRouteForm("Default", new {
controller="YourController", action = "YourFormAction" })) {
}

 

 

 

Hey, that works. Its still notm ideal thouh, as changing routing on a global scale would need you to edit every occurrence of this :( –   
I agree it isn't ideal and may require some work to update all the uses of BeginForm -> BeginRouteForm if being done on an existing webapp, but if you have all your forms set up to use the default route (as specified above) then as long as you keep that route intact any other changes to routing will not affect the forms, so it is a robust solution. –   

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/seesky/archive/2012/05/11/2496652.html

你可能感兴趣的文章
L1-5. A除以B【一种输出格式错了,务必看清楚输入输出】
查看>>
Git一分钟系列--快速安装git客户端
查看>>
bzoj 3160 万径人踪灭 —— FFT
查看>>
poj3254二进制放牛——状态压缩DP
查看>>
使用 ref 和 out 传递数组注意事项
查看>>
三个线程ABC,交替打印ABC
查看>>
flex4.6 图表 在module中 x轴旋转正确的做法
查看>>
LeetCode Binary Tree Preorder Traversal
查看>>
spark_to_kakfa
查看>>
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
查看>>
combobox和textbox中输入数据为非数字leave时的公用事件,只需要在控件的leave事件中选择本事件即可...
查看>>
[原创]如何确保JavaScript的执行顺序 –之jQuery1.5.1与jQuery1.4.4的差异
查看>>
Java生鲜电商平台-源码地址公布与思考和建议
查看>>
一个数据库小题目
查看>>
小学生运算题目生成器说明书
查看>>
20145104张家明 《Java程序设计》第三次实验设计
查看>>
fetch 添加请求头headers
查看>>
【javascript】javascript中function(){},function(){}(),new function(){},new Function()
查看>>
Linux命令—tar
查看>>
BLE GATT 介绍
查看>>