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.

2
  • use window.location Commented Apr 6, 2014 at 16:23
  • You're not doing anything with the result of your $.get Commented Apr 6, 2014 at 16:23

2 Answers 2

5

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

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

More details here

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

Use window.location

window.location = redirectUrl;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.