1

I use C# MVC version 5. I have a form in a modal dialog, which uses a partial view. I want to submit the form and not redirect. I just want the dialog to close. Can this be done?

Here is my code:

<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);
}
Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
Joseph Anderson
  • 4,114
  • 4
  • 45
  • 99

2 Answers2

1

With jQuery you can just serialize the form data and send it to the server in the background and you can close your modal, here is an example: How to use jquery $.post() method to submit form values

If you are using <input type="submit" /> instead of <button type="button">Submit</button> then you'll need to prevent your form from submitting like this: Using JQuery - preventing form from submitting

hujtomi
  • 1,540
  • 2
  • 17
  • 23
1

You can do this with a Bootstrap modal without the jQuery code.

On the page where I want to display the modal. Your modal form (PartialView) will display on top of the view you're already on.

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

Your modal displays in a 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>

}

Charles Owen
  • 2,403
  • 1
  • 14
  • 25