2014-06-25 67 views
0

所以,我試圖在列表頁面(http://example.com:3480/List)上提交一個實際上是Search實現的表單。到目前爲止,我已經做到了這一點:如何在MVC3中連接一個表單與控制器?

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這似乎很好。

現在我想我需要在Global.aspx中定義路由,但不確定。我想要的是在相同的View文件中顯示結果,而不是創建一個新的結果。

在這個時刻,我無法進入Search方法發帖時

+0

一個新的視圖? – MikeSW

+0

@MikeSW用於測試,檢查我是否使用正確的方法發送 – Volatil3

+0

*現在我沒有被髮送到正在發送數據的方法* - 以爲我的問題是誤導性的,我想添加此評論。 –

回答

1

假設你之後,目前只使用默認路由,你是不是達到操作方法的原因是,在「控制器」後綴的路由是隱含的 - 它不應該是你的URL的一部分。

@using(Html.BeginForm("Search","List")) 

另外,關於:

我想是展示,而不是創建一個新的結果在同一視圖文件。

您可以輕鬆地在呼叫指定視圖的名稱爲View方法返回從任何控制器採取行動的具體看法:你爲什麼要通過使用Response.Write,你在哪裏創建

return View("Index"); 
+0

原始問題中添加的Global.asax內容 – Volatil3

+0

應該沒有區別。 –

+0

請原諒?我沒有得到 – Volatil3

相關問題