MVC3中的多种形式

Viv*_*ndi 2 c# asp.net-mvc-3

我有一个带有表单的网页,看起来有点像这样:

@using (Html.BeginForm("MyAction", "Controller", FormMethod.Post))
{
    // html input fields here
    // ...
    // [SUBMIT]
}
Run Code Online (Sandbox Code Playgroud)

当用户按下提交按钮时,将调用以下函数:

public ActionResult MyAction ( string id )
{
    // default action
}

[HttpPost]
public ActionResult MyAction ( MyModel model )
{
    // called when a form is submitted
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,我必须添加另一个表单.但是我如何判断提交的表格是什么?因为两者现在都会以第二种(HttpPost)方法结束......

什么是分离两种表单行为的好方法?请注意,提交表单时,我必须保持同一页面.我无法将自己重定向到另一个页面/控制器.

Iri*_*dio 5

如果我正确理解您的问题,您将有一个页面,其中包含两个表单.作为第一种方法,我将每个表单发布到同一控制器的不同操作.

首先

@using (Html.BeginForm("MyAction", "Controller", FormMethod.Post))
Run Code Online (Sandbox Code Playgroud)

第二

@using (Html.BeginForm("MyAction2", "Controller", FormMethod.Post))
Run Code Online (Sandbox Code Playgroud)

然后,对你的两个遵循DRY原则的行为进行一些重构.

如果相反你需要两个表单发布到同一个动作,那么我会放一个隐藏的输入让我知道一个人被调用.