如何在MVC3中连接窗体和控制器
本文关键字:窗体 控制器 连接 MVC3 | 更新日期: 2023-09-27 18:10:19
所以,我试图在列表页面(http://example.com:3480/List)上提交表单,这实际上是一个搜索实现。到目前为止,我已经这样做了:
index.cshtml
@using(Html.BeginForm("Search","ListController"))
{
<input id=query type=text name=query />
<input id=btnsearch type=submit value=Search />
}
ListController.cs
[HttpPost]
public ActionResult Search(FormCollection collection)
{
Response.Write("We are here");
// Get Post Params Here
string var1 = collection["query"];
Response.Write(var1);
return View();
}
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Details",
"Details/{id}/{orderid}",
new { controller = "Details", action = "Index", id = UrlParameter.Optional, orderid = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
}
点击后,它会转到http://example.com:3480/ListController/Search
,看起来很好。
现在我想我需要在全局中定义route。Aspx,但不确定。我想要的是显示结果在相同的视图文件,而不是创建一个新的。
此刻我无法进入Search
方法后张贴表单
假设你目前只是使用默认路由,你没有到达action方法的原因是你的路由上的"Controller"后缀是隐式的——它不应该是你的URL的一部分。
@using(Html.BeginForm("Search","List"))
此外,关于:
我想要的是显示结果在相同的视图文件,而不是创建一个新的
你可以很容易地从任何控制器动作返回一个特定的视图,通过在调用View
方法中指定视图的名称:
return View("Index");