共享视图中的ASP.NET MVC表单POST

时间:2015-01-07 19:12:29

标签: c# asp.net-mvc

〜/查看/ _TopNavigation.cshtml:

<form method="POST" action="/">
    @Html.AntiForgeryToken()
    @Html.DropDownList("SelectionId", null, htmlAttributes: new { @class = "form-control", onchange = "this.form.submit();" })
</form>

〜/控制器/ BaseController.cs

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);

        // SelectionIdlogic here
    }

在处理新的SelectionId值时,我希望将页面路由到当前控制器/操作,而不是发布到&#34; /&#34 ;.

2 个答案:

答案 0 :(得分:-1)

使用:

@using (Html.BeginForm())
{}

这会将表单发布到与当前视图的POST操作相对应的操作。

答案 1 :(得分:-1)

using (Html.BeginForm("Action", "Controller", FormMethod.Post))
    {
    @Html.AntiForgeryToken()
    @Html.DropDownList("SelectionId", null, htmlAttributes: new { @class = "form-control", onchange = "this.form.submit();" })
}