我有一个带有表单的网页,看起来有点像这样:
@using (Html.BeginForm("MyAction", "Controller", FormMethod.Post))
{
// html input fields here
// ...
// [SUBMIT]
}
当用户按下提交按钮时,将调用以下函数:
public ActionResult MyAction ( string id )
{
// default action
}
[HttpPost]
public ActionResult MyAction ( MyModel model )
{
// called when a form is submitted
}
现在我的问题是,我必须添加另一个表格。但是我怎么知道提交了哪个表单呢?因为现在两者都将以第二种 (HttpPost) 方法结束......
将两种表单操作分开的好方法是什么?请注意,提交表格时,我必须留在同一页面上。我无法将自己重定向到另一个页面/ Controller 。
最佳答案
如果我正确理解您的问题,您将看到一个包含两个表单的页面。 作为第一种方法,我会将每个表单发布到同一 Controller 的不同操作。
第一个
@using (Html.BeginForm("MyAction", "Controller", FormMethod.Post))
第二个
@using (Html.BeginForm("MyAction2", "Controller", FormMethod.Post))
然后,对您的两个操作进行一些重构以遵循 DRY principle .
如果相反,您需要两个表单都发布到同一操作,那么我将放置一个隐藏的输入,让我知道哪个被调用了。
关于c# - MVC3中的多种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973999/