如何放置两个表单提交按钮

如何放置两个表单提交按钮

问题描述:

我有两个在一个表单提交按钮。我想打电话给在两个按钮不同的操作。有什么办法做到这一点不使用JavaScript。

I have two submit buttons in one form. I want to call different actions in both buttons. Is there any way to accomplish this without using JavaScript.

调用不同的动作也不是没有可能的JavaScript。你可以调用相同的控制器动作,因此这个动作内确定哪个按钮是使用name属性pressed:

Calling different actions is not possible without javascript. You could call the same controller action and inside this action determine which button was pressed using the name property:

<% using (Html.BeginForm()) { %>
    <input type="submit" name="save" value="Save" />
    <input type="submit" name="update" value="Update" />
<% } %>

和在你的控制器动作:

[HttpPost]
public ActionResult Index(string save)
{
    if (!string.IsNullOrEmpty(save))
    {
        // the save button was pressed
    }
    else
    {
        // the update button was pressed
    }
    return View();
}