【问题标题】:Ajax.BeginForm inside Html.BeginForm multiple submit button mvcAjax.BeginForm里面的Html.BeginForm多个提交按钮mvc
【发布时间】:2017-01-22 11:07:10
【问题描述】:

我的页面上有嵌套表单。

@using (Html.BeginForm())
{
    @Html.TextBoxFor(model => model.ComapnyName, new { @class = "form-control", placeholder = @Resources.Customers.ComapnyName })
    @Html.ValidationMessageFor(model => model.PhoneNuComapnyNamember, "", new { @class = "text-danger" })
    // other stuff
    <button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target=".modal-lg-customer-departments">
        <i class="fa fa-plus"></i> Add
    </button>
    //bootstrap modal
    @using (Ajax.BeginForm("CreateCustomersDepartments", "Customers", null, new AjaxOptions
    {
        HttpMethod = "Post",
        UpdateTargetId = "departmentsId",
        OnSuccess = "$('#departmentsModal').modal('hide')"
    }))
    {
        @Html.TextBoxFor(model => model.Name, new { @required = "require", @class = "form-control", placeholder = Resources.Common.Name })
        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        // other stuff
        //Create departments
        <input type="submit" value="@Resources.Common.Save" class="btn btn-success" name="CreateDepartments" />   
    }   
    //Create company
    <input type="submit" value="@Resources.Common.Save" class="btn btn-success" name="Create" />   
}

当我单击提交按钮(为主表单创建)时,需要验证器保留模态字段(depoartments 添加)。我从添加部门开始,然后单击 modalpopup 主窗体上的提交按钮,保留验证器。

我试过了

How to have multiple submit buttons in your MVC forms

但页面无效,代码未到达控制器。 在asp.net(web forms)上我使用validationGroup,如何得到同样的效果

【问题讨论】:

  • 嵌套表单是无效的 html,不受支持。将内部模态窗体移动到主窗体之后
  • 它解决了我的问题!谁给的链接 这是少数多重提交按钮的正确方法?如果有更好的解决方案?
  • @18666 该链接显示了如何使用多个提交按钮而不是多个嵌套表单。
  • 正如@CodingYoshi 所指出的,您误解了链接中的代码。它不使用嵌套表单 - 它使用按钮的 action 属性将一个表单提交给不同的方法。

标签: ajax asp.net-mvc forms


【解决方案1】:

Ajax 表单呈现为表单标记,这意味着您将一个表单嵌套在另一个表单中,这是无法做到的。

如果你想在一个视图上使用多个提交按钮,你可以这样做

  <button type="submit" name="button1" id="button1" class="btn btn-success" formaction = '@Url.Action("ActionMethod", "ConrollerName")'>View1</button> 
  <button type="submit" name="button2" id="button2" class="btn btn-success" formaction = '@Url.Action("ActionMethod", "ConrollerName")'>View2</button> 

以上只是一个示例或一种周转方式,通过使用“formaction”属性在单个视图上使用不同的提交按钮来调用不同的控制器/操作方法。

您还可以将 JSON 请求与 jQuery 一起使用。

Take a look at this thread for more details on JSON request.

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    相关资源
    最近更新 更多