模式对话框无重定向

时间:2019-01-07 21:44:28

标签: c# asp.net-mvc

我使用C#MVC版本5。我在模态对话框中有一个窗体,该窗体使用部分视图。我想提交表格,而不是重定向。我只想关闭对话框。能做到吗?

这是我的代码:

<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header bg-success">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h5 class="modal-title">@title</h5>
        </div>
        @using (Html.BeginForm("Edit", "Region", FormMethod.Post, new { onsubmit = "return validateForm();", @class = "form-horizontal" }))
        {
            <div class="modal-footer">
                <button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-success">Submit form</button>
            </div>
        }        
    </div>
</div>

public ActionResult Edit(int id)
{
    RegionViewModels regionViewModels = MakeRegionViewModels(id);
    return PartialView("_InsertTaxRegion", regionViewModels);
}

2 个答案:

答案 0 :(得分:1)

使用jQuery,您只需序列化表单数据并在后台将其发送到服务器,就可以关闭模​​式,这是一个示例: How to use jquery $.post() method to submit form values

如果您使用的是<input type="submit" />而不是<button type="button">Submit</button>,则需要阻止表单提交,如下所示: Using JQuery - preventing form from submitting

答案 1 :(得分:1)

您可以使用Bootstrap模态来执行此操作,而无需使用jQuery代码。

在我要显示模式的页面上。您的模式窗体(PartialView)将显示在您已经在视图上。

<button id="btn" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-       target="#myModal">Open Modal</button>
 <!-- Modal -->
<div id="myModal" class="modal fade" role="dialog" data-toggle="modal">
    <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
            @Html.Partial("Modal");
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" 
              data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

您在PartialView中显示的模式...

@model NCR.Models.Employee
@{
    ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
   <div class="form-horizontal">
   <div class="form-group">
       @Html.LabelFor(model => model.FIRST_NAME, htmlAttributes: 
       new { @class = "control-label col-md-2" })
       <div class="col-md-10">
        @Html.EditorFor(model => model.FIRST_NAME, 
        new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FIRST_NAME, "", 
        new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.LAST_NAME, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.LAST_NAME, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.LAST_NAME, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Update" class="btn btn-default" />
    </div>
</div>

}