JTunney
JTunney

Reputation: 904

Call Action Method, close dialog and refresh parent

I may be thinking about this incorrectly since I am new to MVC5.

I have a CustomerInfo.cshtml view that lists CustomerContacts on it. Next to them is a '+' icon that will open a dialog window and allow them to add a new CustomerContact.

On dialog open I call CustomerContactController Create GET method (passing it the customerId setting it to my model and passing it to the view).

On save I call CustomerContactController Create POST method set the values and save to db.

At this point how can I close the dialog window and refresh the parent CustomerInfo.cshtml page so the new contact will appear in the list. All of the Action methods require me to return something.

I am opening my dialog from CustomerInfo.cshtml view like this:

@(Html.Kendo().Window()
    .Name("windowContact")
    .Title("New Customer Contact")
     .LoadContentFrom("Create", "CustomerContact", new { customerId = Model.Id })
    .Draggable()
    .Width(680)
    .Visible(false)
    .Events(events => events.Open("centerWindow"))
)

In the dialog window I have a razor form. Here is the bottom of the dialog:

            <input type="submit" value="Save" id="btnSave" class="btn btn-primary" />
            <input type="button" value="Cancel" id="btnCancel" class="btn btn-default" />
}

<script>
    $("#btnCancel").click(function () {
        $("#windowContact").data("kendoWindow").close();
    });
</script>

Here is what I am currently doing and it is taking me back to the CustomerInfo view but with a refresh. Is the only way to do it smoothly using AJAX?

return RedirectToAction("CustomerInfo", "Customer", new { id = custContact.CustomerId });

Upvotes: 0

Views: 1307

Answers (1)

BabyDuck
BabyDuck

Reputation: 1249

Post the form-data of Create view through jQuery Ajax. Like:

$.ajax({
  url: '/Create/CustomerContact',
  type: 'post',
  contentType: "application/x-www-form-urlencoded",
  data: $form.serialize(),
  success: function (data, status) {
    // here close the window and refresh parent  
    // if your new window is opened in iframe  
    window.parent.location.reload();
    $("#windowContact").data("kendoWindow").close();

    // if your new window is opened by window.open() method.
    window.opener.location.reload();
    self.close();
  },
  error: function (xhr, desc, err) {
    alert("Desc: " + desc + "\nErr:" + err);
  }
});

Hope it works for you, thanks.

Upvotes: 1

Related Questions