【问题标题】:Multiple submit buttons with values passed from buttons具有从按钮传递的值的多个提交按钮
【发布时间】:2017-06-06 03:51:02
【问题描述】:

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

我可以轻松创建多个 ActionResult,但对我来说感觉太复杂了,如果我必须更改测试连接中的某些内容,我就必须全部更改。所以我希望只有一个用于 TestConnection 的 ActionResult 方法加上一个 Save ActionResult 都在同一个表单中。

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

当我单击“测试连接”按钮时,我仍然会收到 ApplicationSettings 模型以及 DatabaseSystems 枚举,并且根据从按钮收到的枚举,我会进行必要的连接。

下面是sn-ps的代码,但我就是不能让它们工作。

尝试的方法: 1.空白表单动作,提交按钮的名称分别为“Save”和“TestConnection”。 - 保存作品。 TestConnection 不起作用,说找不到资源。 2.表单动作指向“TestConnection”,TestConnection按钮名称设置为 “数据库”,而值设置为 @DatabaseSystems.xxx,保存按钮设置为“action:Save”,添加了用于保存的 ActionResult 方法[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
}

【问题讨论】:

  • 那你有什么问题?
  • 你是什么意思不损害对方?而且你有@using (Html.BeginForm()),这意味着你的表单将提交回与 GET 方法同名的 POST 方法(你没有说那是什么) - 你需要一点 javascript 来回发到单独的方法(尽管不清楚为什么需要单独的方法)
  • @Stephen Muecke,更新帖子。
  • 我想远离一种 ActionResult 方法并在内部进行隔离。相反,我希望视图本身为我进行隔离,因此当我单击 TestConnection 时,它会直接转到 TestConnection 方法等。
  • 我之前试过:Controller public ActionResult TestConnectionSystem1(ApplicationSettings model, DatabaseSystems database) public ActionResult TestConnectionSystem2(ApplicationSettings model, DatabaseSystems database) public ActionResult TestConnectionSystem3(ApplicationSettings model, DatabaseSystems database) 然后查看 &lt;button type="submit" class="btn btn-warning pull-right" name="action:TestConnectionSystem1" id="@DatabaseSystems.System1" data-toggle="collapse" data-target="#loading"&gt;&lt;span class="glyphicon glyphicon-link"&gt;&lt;/span&gt; Test Connection&lt;/button&gt;

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


【解决方案1】:

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

检查连接

<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
     }
      }

【讨论】:

  • 恐怕这个不太行,伙计。控制器无法识别正在调用的方法。
  • 我已经修改了我的答案,请检查。
  • @Nlts577,感谢您的建议,但这正是我想要避免的。我想让 MVC 为我做隔离,而不是我做这样的代码。 :D
【解决方案2】:

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

查看

@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 方法。

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2015-07-11
    相关资源
    最近更新 更多