表单在_Layout.cshtml中提交

时间:2013-09-21 21:56:24

标签: asp.net-mvc asp.net-mvc-4

我在布局文件中放置了一个搜索框,以便使用布局的所有页面都具有布局。当用户点击按钮进行搜索时,我在哪里处理此表单中的提交/操作?

这对于从_Layout.cshtml页面启动的操作有何用处?

1 个答案:

答案 0 :(得分:4)

与在普通视图中使用表单没有什么不同。您只需编写一个响应POST请求的操作,并确保将表单发布到该操作。

在_Layout.cshtml中

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

Inside HomeController(可以是任何其他控制器)

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