BeginForm渲染一个空白的action方法
问题是每当我在 BeginForm 中对 Action 和 Controller 进行硬编码时,都会导致空白的操作方法。
我很困惑。
已从 HomeController 和 Index 操作方法调用下面的视图。
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "first" }))
{}
结果
<form id="first" method="post" action="/Home"></form>
已从 HomeController 和 Page 操作方法调用下面的视图。
@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { id = "first" }))
{}
结果
<form id="first" method="post" action=""></form>
路由
routes.MapRoute(
"RootUrlWithAction",
"Home",
new
{
controller = "Home",
action = "Index",
name = "home",
id = UrlParameter.Optional
}
);
routes.MapRoute(
"DynamicPages",
"{name}/{id}",
new
{
controller = "Home",
action = "Page",
id = UrlParameter.Optional
}
);
routes.MapRoute(
"EmptyUrl",
"",
new
{
controller = "Home",
action = "Index",
name = "home"
}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{name}/{id}",
new
{
controller = "Home",
action = "Index",
name = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
控制器操作
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Page(String name)
{
return View();
}
[HttpPost]
public ActionResult Edit(Order orderVm)
{
var a = orderVm;
string errorMessage = "hehehe";
return Json(new Order { Message = errorMessage });
}
}
The problem is whenever I hardcode Action and Controller in BeginForm it results in a blank action method.
I am confused.
Below view has been invoked from HomeController and Index action method.
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "first" }))
{}
Result
<form id="first" method="post" action="/Home"></form>
Below view has been invoked from HomeController and Page action method.
@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { id = "first" }))
{}
Result
<form id="first" method="post" action=""></form>
Routing
routes.MapRoute(
"RootUrlWithAction",
"Home",
new
{
controller = "Home",
action = "Index",
name = "home",
id = UrlParameter.Optional
}
);
routes.MapRoute(
"DynamicPages",
"{name}/{id}",
new
{
controller = "Home",
action = "Page",
id = UrlParameter.Optional
}
);
routes.MapRoute(
"EmptyUrl",
"",
new
{
controller = "Home",
action = "Index",
name = "home"
}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{name}/{id}",
new
{
controller = "Home",
action = "Index",
name = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
Controller Actions
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Page(String name)
{
return View();
}
[HttpPost]
public ActionResult Edit(Order orderVm)
{
var a = orderVm;
string errorMessage = "hehehe";
return Json(new Order { Message = errorMessage });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尝试使用这个可以从 NuGet 下载的工具来调试您的路由。
http://haacked.com/archive/2011/04/13/routedebugger -2.aspx
让我知道它如何为您工作。
You should try to debug your routes with this tool that you can download from NuGet.
http://haacked.com/archive/2011/04/13/routedebugger-2.aspx
Let me know how it works for you.