I have a view in which there is a registration form generated using the Ajax.BeginForm () helper:

<div id="register_form"> @using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "register_form", OnFailure = "onFailure", OnSuccess = "onSuccess", OnBegin = "onBegin" } , new { @class = "well form-horizontal" })) { // Html-код формы } </div> 

In case of successful execution of the Ajax request, the onSuccess script is triggered, the code of which is shown below:

 var onSuccess = function (result) { // Код обработчика } 

How in the onSuccess script to prevent updating the element with the id "register_form", which is specified as UpdateTarget in the AjaxOptions helper that generates the form?

An action similar to the preventDefault () method is required.

UpdateTarget must be specified in this form, since both PartialView can come from the server to update the form, as well as a JSON message about the form filling error.

    1 answer 1

    Send an error message with the code 400, not 200 - and catch it in the onError handler.

    • I did not clarify - the error is not in the sense of an exception, but in the sense of not passing validation of the form on the server. And the problem arises when I return a JSON response from the server, which I display in a pop-up message, while trying to replace the html element specified in UpdateTargetId, which is why the form simply disappears, which looks very unprofessional. Therefore, it becomes necessary to cancel the update of the element specified in UpdateTarget from the onSuccess method. - Nick
    • How does an error in the sense of an exception differ so much from an error in the sense of validation that for the second one it is impossible to use code 400? - Pavel Mayorov
    • The main problem is not to return the error message, but the need to cancel the update of the html element specified in UpdateTargetId, if it is at all possible. - Nick
    • @Nick so if you return the error - it will not be updated the same - Pavel Mayorov
    • Mayorov, it is possible that I need to return a success message from the server, which I will put in a pop-up window in the onSuccess method. If at the same time pass the error, then in my opinion it will be a crutch. - Nick