How to redirect to new page(along with model) on JQuery ajax call in MVC3

前端 未结 2 693
逝去的感伤
逝去的感伤 2021-01-16 06:48

I have forgot password page, where the user enters the username and clicks the \'Validate\' button to check which group he is in. Based on the group we need to display the d

相关标签:
2条回答
  • 2021-01-16 07:36

    This is how we need to do. We can't pass the model, we can only pass the parameters in the redirection of a link. And we must use link.replace as you can pass the variables directly into the Url.Action

    person = {UserName: $("#UserName").val(), Phone: $("#Phone").val() }
     $.ajax({
            type: 'POST',
            url: '@Url.Action("ForgotPassword", "Customer")',
            data: person,
            dataType: 'json',
            success: function (data) {
                //This is the place I need help
                if(IsDataValid){
                  var link = "@Url.Action("Actionname", "Controllername", new { username = "1", phone ="2" })";
                       link = link.replace("1", data.username);
                       link = link.replace("2", data.phone);
                       alert(link);
                       window.location.href= link ;
                }
                else{
                    //Show an error message without hiding the div
                }
            },
            failure: function () {                   
            }
        });
    
    0 讨论(0)
  • 2021-01-16 07:36

    Addressing the part of your question that is answerable right now:

    if(IsDataValid){
      // redirect to new page(
    }
    else{
        //Show an error message without hiding the div
    }
    

    To redirect, use window.location.replace('url to new page');.

    For the "error message" part, I would either use jQuery UI to display a dialog, or have a hidden div that you insert error messages into, perhaps like so:

    $('#errors').html(data.Errors).show();
    

    This assumes you have some sort of property called Errors that contains validation messages on RegistrationModel.

    0 讨论(0)
提交回复
热议问题