0

I am currently working with mvc. I have a page with more than one button on, which does complicate things a little. However the button I am using calls a jquery function to post data to the controller. This works as expected and data reaches the action and the action does everything expected except redirect.

The function posts data to the controller, or allows the controller to see the values and build up a url to then redirect to. However the redirect doesnt work, and the function calls a success function as expected. If I put the data return into an alert the code for the page is returned in the alert window not the url.

Here is the code:

 <input type="submit" value="Assign" name="Assign" onclick="doRedirect()" />

function doRedirect() {
    var action = '@Url.Action("Redirect")';
    //alert(action);
    var opt = {
        type: "POST",
        data: {
            Team: $('#Team option:selected').val()
        },
        url: action,
        success: RedirectSuccess

    };
    jQuery.ajax(opt);
}

    function RedirectSuccess(data) {
        if (data != undefined) {   
             data;          
             }
    }

public ActionResult Redirect(string Team)
    {

        var hostName = "http://localhost/test/testpage.aspx?";
        var team = "&Team=" + Team;


        var filterUrl = Team;


        return Redirect(filterUrl);**//this doesnt work**

    }

2 Answers 2

2

Instead of sending back a redirect result from the action, try sending back the URL you want to redirect to. On the client side, you read the response of the request, and do the redirect by setting window.location.href to the URL you get back from the server.

In your action, you could return the URL as JSON for instance:

return Json(new { url: filterUrl });

And in your success callback, you do this to redirect:

if (data !== undefined && data.url !== undefined) {   
      window.location.href = data.url;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is what I did instead.

public string Redirect(string Team)
{

    var hostName = "http://localhost/test/testpage.aspx?";
    var team = "&Team=" + Team;


    var filterUrl = hostname + Team;


    return filterUrl;
}

function RedirectSuccess(data) {
        if (data != undefined) {
            window.location = data;          
             }
    }

and on my search success

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.