3

How to work with onFailure and OnSuccess methods. Currently I add onFailure js method but its doesnot executes. Maybe I forgot to add some scripts. (I added only jquery.unobtrusive.ajax.js).

I have to return onFailure partialview in modal window.

Code in Controller

  [HttpPost]
        public ActionResult RequestPassword(RequestForPasswordViewModel passwordRequestViewModel)
        {
             if (!ModelState.IsValid)
            {
               Response.StatusCode = (int)HttpStatusCode.BadRequest;
               return PartialView("RequestForPassword", passwordRequestViewModel);
            }

Code in partial view

@model YouCapital.Web.Models.ViewModels.RequestForPasswordViewModel

<div>

    @using (Ajax.BeginForm("RequestPassword", FormMethod.Post, new AjaxOptions()
    {
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        UpdateTargetId = "forgotPassword",
        OnFailure = "JsonRequestForPassword_OnFailure",
        OnSuccess = "JsonRequestForPassword_OnSuccess"

    }, new { @class = "form-inline" }))
    {



        <fieldset>
            <legend>Recover password</legend>
            <div class="form-group">
                @Html.Label("Enter your email")
                @Html.TextBoxFor(x => x.Email)
            </div>

            <input type="submit" class="btn btn-default" value="Recover"/>

        </fieldset>


         @Html.ValidationSummary()
    }




</div>


@section scripts
{
    <script>
        function JsonRequestForPassword_OnFailure() {
            console.log('fail');

        }
        function JsonRequestForPassword_OnSuccess() {
            console.log('success');
        }
    </script>
}

By the way, I do not know if its a problem, but it is in bootstrap modal and as a tabpanel

  • Possibly duplicate question http://stackoverflow.com/questions/5517813/how-to-use-ajax-beginform-onsuccess-and-onfailure-methods – Oleh Oct 30 '14 at 11:44
  • Show the code for the onFailure callback. And the controller code that will trigger it. –  Oct 30 '14 at 11:52
  • I have seen this post and I have already script method that have to be executed onFailure. But its returns only PartialView (in full screen, not in modal) and doesnot executes javascript methods. I added scripts as I viewed in other posts, but does not work. – Solomiia Kalinchuk Oct 30 '14 at 11:54
  • Keep in mind that the `OnFailure` is for regular http error response, i.e. the response is a 404, 500, 503, etc. In your case, you are returning a valid 200 response, and for sure the `OnFailure` won't be triggered. – tweray Oct 30 '14 at 12:04
  • Even if I added another response status, 'onFailure' method was not triggered. As well without it, 'onSuccess' method has to be triggered, but it wasn't – Solomiia Kalinchuk Oct 30 '14 at 12:07
  • @СоломіяКвасниця Can you show how did you `added another response status` in your controller code? Also did you mean that your `OnSuccess` also never worked? – tweray Oct 30 '14 at 12:17
  • @tweray I already edited my code – Solomiia Kalinchuk Oct 30 '14 at 12:21
  • Just to confirm did you add `jquery` above `jquery.unobtrusive.ajax.js`? – tweray Oct 30 '14 at 12:27
  • @tweray Yes, I added it above – Solomiia Kalinchuk Oct 30 '14 at 12:34
  • @tweray, Yes, neither onFailure and onSuccess didn't work at all – Solomiia Kalinchuk Oct 30 '14 at 12:48

1 Answers1

1

try these small things, add these scripts `

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.js")"></script>`

and in document.ready

 $(document).ready(function () {
    $.validator.unobtrusive.parse('#form');
});

and for form

  @using (Ajax.BeginForm("Controller Method", "Controller Name", new AjaxOptions { HttpMethod = "POST",      OnSuccess = "JsonRequestForPassword_OnSuccess", UpdateTargetId = "forgotPassword" }, new { @id = "form" }))

update target id should be any div or place holder that has your form

tspga
  • 117
  • 1
  • 13
  • thanks, but it is not a solution for me. Of course if I added manually required attribute in TextBoxFor , it will not pass empty input (even with out your validator), but I have attributes in my model in c# code and it would be great if onFailure and onSuccess methods worked. But they don't . – Solomiia Kalinchuk Oct 30 '14 at 13:18
  • have you given the controller name?? in ajax.beginform – tspga Oct 30 '14 at 13:27
  • At first I don't as Controller name is required if I call action from other controller, but then I tried it and still it does not work for me – Solomiia Kalinchuk Oct 30 '14 at 13:39