多个提交按钮,其值从按钮

时间:2017-06-06 03:51:02

标签: c# asp.net-mvc forms razor

我构建表单的方式是有多个“测试连接”提交按钮和“保存”提交按钮,以保存每个测试连接的所有凭据和URL。我见过一些建议的解决方案,但我不想做ajax或javascript。所以我想知道这是否可行。顺便说一句,我想通过提交按钮将值传递给控制器​​作为枚举值,并在我的控制器中接收枚举。

我可以很容易地制作多个ActionResults,但它对我来说感觉很复杂,如果我必须在测试连接中改变一些东西,我将不得不改变所有。所以我想在同一个表单中只有一个TestConnection的ActionResult方法和一个Save ActionResult。

单击“保存”按钮时,所有凭据都将作为ApplicationSettings模型发送到控制器。

当我单击Test Connection按钮时,我仍然会收到ApplicationSettings模型和DatabaseSystems枚举,并且根据从按钮收到的枚举,我会进行必要的连接。

以下是代码段,但我无法让它们正常工作。

尝试过的方法: 1.空白表单操作,提交按钮的名称为“保存”和“TestConnection”。 - 保存工作。 TestConnection不起作用说无法找到资源。 2.表单操作指向“TestConnection”,TestConnection按钮名称设置为 “database”,同时值设置为@ DatabaseSystems.xxx,Save按钮设置为“action:Save”,ActionResult方法为Save添加[MultipleButton(Name = "action", Argument = "Save")] - TestConnection工作,Save不起作用说不明确的请求。

我似乎无法弄清楚配置是否同时起作用。 :(

查看

@using Models.Enums
@model MyApp.Data.ApplicationSettings

@Html.AntiForgeryToken()
@using (Html.BeginForm())
{
<div class="row">
    <div class="col-md-2">
        @Html.EditorFor(model => model.con1_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_un) } })
    </div>
    <div class="col-md-2">
        @Html.EditorFor(model => model.con1_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_pw) } })
    </div>
    <div class="col-md-7">
        @Html.EditorFor(model => model.con1_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_url), @rows = "1", @style = "max-width:520px !important" } })
    </div>
    <div class="col-md-1 pull-right">
        <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System1" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>
    </div>
</div>
<div class="row">
    <div class="col-md-2">
        @Html.EditorFor(model => model.con2_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_un) } })
    </div>
    <div class="col-md-2">
        @Html.EditorFor(model => model.con2_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_pw) } })
    </div>
    <div class="col-md-7">
        @Html.EditorFor(model => model.con2_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_url), @rows = "1", @style = "max-width:520px !important" } })
    </div>
    <div class="col-md-1 pull-right">
        <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System2" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>
    </div>
</div>

<div class="row">
    <div class="col-md-2">
        @Html.EditorFor(model => model.con3_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_un) } })
    </div>
    <div class="col-md-2">
        @Html.EditorFor(model => model.con3_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_pw) } })
    </div>
    <div class="col-md-7">
        @Html.EditorFor(model => model.con3_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_url), @rows = "1", @style = "max-width:520px !important" } })
    </div>
    <div class="col-md-1 pull-right">
        <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System3" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>
    </div>
</div>

... and many more test connections for different systems

<button type="submit" class="btn btn-success pull-right" name="Save" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
}

控制器

[HttpPost]
ActionResult TestConnection(ApplicationSettings model, DatabaseSystems database)
{
    // depending on the DatabaseSystems enum passed, then I'll do the necessary connection check.
{
[HttpPost]
ActionResult Save(ApplicationSettings model)
{
    // Save credentials entered in all the input fields above for con1, con2 & con3.
}

模型

[Serializable]
public class ApplicationSettings
{
    public string con1_un { get; set; }
    public string con1_pw { get; set; }
    public string con1_url { get; set; }

    public string con2_un { get; set; }
    public string con2_pw { get; set; }
    public string con2_url { get; set; }

    public string con3_un { get; set; }
    public string con3_pw { get; set; }
    public string con3_url { get; set; }

    ... and many more systems
}

枚举

public enum DatabaseSystems
{
    System1,
    System2,
    System3,
    ... and many more systems
}

2 个答案:

答案 0 :(得分:0)

对多个提交按钮使用具有不同参数的单个动作名称。并且两个按钮中的名称相同但值不同

用于检查连接

<button type="submit" class="btn btn-warning pull-right" name="save" value="@DatabaseSystems.System3" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>

用于保存

<button type="submit" class="btn btn-warning pull-right" name="Save" value="save" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Save</button>


    [HttpPost]
    ActionResult Save(ApplicationSettings model,string Save)
                {
    if(Save=="Testconnection")
    {
     //do your code on save
    }
    if(save="Save")
    {
     //do your code on save
     }
      }

答案 1 :(得分:0)

终于明白了。虽然@Stephen Muecke并没有真正提供直接的解决方案,但我应该说他让我重新考虑再次使用编队,所以我还是要感谢他。谢了哥们!以下是我如何解决它:

查看

@using Models.Enums
@model MyApp.Data.ApplicationSettings

@Html.AntiForgeryToken()
@using (Html.BeginForm())
{
    ...
    <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System1" formaction="Admin/TestConnection" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>
    ...
    <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System2" formaction="Admin/TestConnection" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span>  Test Connection</button>
}

<强>控制器

public ActionResult TestConnection(ApplicationSettings model, DatabaseSystems database)
{
    // at the end, instead of putting return View() or return("Index", model), I used below code
    return Redirect("/[ControllerName]");
{

...这样,formaction将为我调用ActionResult方法,同时保持按钮的name属性可用于其他目的,例如将其作为参数传递给我的TestConnection方法。