Ajax.BeginForm里面有Html.BeginForm多个提交按钮mvc

时间:2017-01-22 11:07:11

标签: ajax asp.net-mvc forms

我在页面上嵌套了表单。

@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 add)。我从添加部门开始,然后单击modalpopup上的提交按钮,主窗体保留验证器。

我试过

How to have multiple submit buttons in your MVC forms

但页面无效代码未来到控制器。 在asp.net(网络表单)上我使用validationGroup,如何获得相同的效果

1 个答案:

答案 0 :(得分:0)

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'属性调用不同的Controller / Action方法。

此外,您可以使用jQuery的JSON请求。

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

希望这有用。