Randy Minder
Randy Minder

Reputation: 48402

Java Script / jQuery - How to redirect to another page

I have an MVC 5/C# application. I have a button on a form that allows the user to delete a record from the DB. When clicked the button calls the JS function below. The HTML that allows the user to delete the current row is:

 <a class="btn btn-default" href="javascript:DeleteLocation(@Model.RowId);">Delete</a>

The JS function looks as follows:

function DeleteLocation(rowId) {
            var url = "/UserLocation/Delete";
            var redirectUrl = "/UserLocation/Index"

            $.get(url, { rowId: rowId }, function () {
                $.get(redirectUrl);
                });
        }

My C# Delete method looks as follows:

[Authorize]
public void Delete(int rowId)
{
    UserLocationData userLocationData = new UserLocationData();
    userLocationData.Delete(rowId);
}

Half of this is working perfectly. My C# method is getting called when the user clicks the button. However, after the delete, the page I want to redirect to isn't getting displayed. I tried putting a RedirectToAction in the C# method but that didn't work either. I don't have a large amount of experience with JS or jQuery so it's quite possible my JS is just wrong. Or there could be a much better way to do this.

Upvotes: 0

Views: 81

Answers (2)

VtoCorleone
VtoCorleone

Reputation: 17203

Use window.location

window.location = redirectUrl;

Upvotes: 1

Brad Faircloth
Brad Faircloth

Reputation: 327

window.location should be used to send the user to another page.

window.location.replace("http://yourwebsite.com");

More details here

Upvotes: 5

Related Questions