2013-09-21 51 views
1

我在佈局文件中放置了一個搜索框,以便所有使用佈局的頁面都有佈局。當用戶點擊按鈕進行搜索時,我在哪裏處理來自此表單的提交/操作?表單在_Layout.cshtml中提交

這是否假設適用於從_Layout.cshtml頁面啓動的操作?

回答

4

這與在普通視圖中使用表單沒有什麼不同。您只需編寫一個響應POST請求的操作,並確保將表單發佈到該操作。

在_Layout.cshtml

@using(Html.BeginForm("Search", "Home")) 
{ 
    ... 
} 

裏面的HomeController(它可以是任何其他控制器)

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult Search(SearchModel model) 
    { 
     //search implementation 
    }   
}