0

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.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358

2 Answers2

5

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

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

More details here

Community
  • 1
  • 1
Brad Faircloth
  • 337
  • 1
  • 7
  • replace removes the current page from browser history and prevents the use of the back button to return. That is useful if you want to prevent a double post. Considering that you are deleting something from a database, replace seems like the best approach. – Brad Faircloth Apr 06 '14 at 16:40
1

Use window.location

window.location = redirectUrl;
VtoCorleone
  • 16,813
  • 5
  • 37
  • 51