一个表单上的多个按钮

时间:2017-08-04 13:23:32

标签: asp.net-mvc

我搜索了stackoverflow和谷歌,并找到了很多关于这个主题的文章,但我似乎仍然无法知道我做错了什么!我真的感到绝望。例如。 How to handle two submit buttons on MVC view

我只是想在我看来有三个按钮。我确实在控制器中执行了操作,但字符串比较不起作用。出于某种原因,我无法达到我的断点。

这是我的代码: 视图:

@using RequestDB7mvc.Models
@model MainViewModel
@{
    ViewBag.Title = "Main";
}
<div class="form-group">
@using (Html.BeginForm("Search4", "Requests", FormMethod.Post))
{
    <input type="submit" value="Save" name="command" />
    <input type="submit" value="Done" name="command" />
    <input type="submit" value="Cancel" name="command" />
}

控制器:

[HttpPost]
public ActionResult Search4(MainViewModel model, string command)
{
    if (command.Equals("Save"))
    {
        // Call action here...
        string f = "break";
    }
    else if (command.Equals("Done"))
    {
        // Call another action here...
        string f = "break";
    }
    else if (command.Equals("Cancel"))
    {
        // Call another action here...
        string f = "break";
    }

    ViewBag.Msg = "Details saved successfully.";
    return View();
}

1 个答案:

答案 0 :(得分:0)

您只需将command添加到模型中即可。

public class MainViewModel
{

    ...

    public string command { get; set; }

    ...
}

然后在您的控制器中,从模型中删除命令参数和句柄:

public ActionResult Search4(MainViewModel model)
{
    if (model.command.Equals("Save"))
    {
        // Call action here...
        string f = "break";
    }
    else if (model.command.Equals("Done"))
    {
        // Call another action here...
        string f = "break";
    }
    else if (model.command.Equals("Cancel"))
    {
        // Call another action here...
        string f = "break";
    }

    ViewBag.Msg = "Details saved successfully.";
    return View();
}