Bootstrap modal form does not close after submit

后端 未结 4 1998
暗喜
暗喜 2021-01-02 20:39

What I need to do is to show a popup to add a new record to my database, im using bootstrap 3 and I love it because im not using a single line of jquery, and I have really n

4条回答
  •  轮回少年
    2021-01-02 20:41

    This is a pretty common fundamental problem with this approach.

    What we have is an AJAX form that, if the form fails to process, then the partial is re-rendered and returned to the browser so that the original form can be replaced with the new one. If the form succeeds, you want the browser to redirect to a new page but the problem is that the browser is expecting a page returned that it can insert into the modal.

    The problem is that the approach is a dichotomy. That is, you need to either do a full page refresh or an AJAX request, but not both in which the appropriate logic is determined by the server.

    One solution to this problem:

    One thing that I can think of is to change the server's response to a Bad Request (400) when you are returning the partial to indicate that, although you are returning some HTML, the process did not complete successfully.

    This means that you can take advantage of the OnFailure AjaxOption. If the result is a failure, then take the response data and insert it as normal in to the modal.

    However if the status is 200 OK then return a URL and take advantage of OnSuccess and perform a simple javascript redirect to the URL.

提交回复
热议问题