Randy Minder
Randy Minder

Reputation: 48522

MVC RedirectToAction not working as expected

I have web page with the following HTML:

   <div class="row">
        @Html.ActionLink("Delete Study", "DeleteStudy", "Study", new {topic = @Model.Study.PartitionKey, subtopic = @Model.Study.RowKey}, new { @class = "btn btn-primary" })
        @Html.ActionLink("View Studies", "StudyList", "Study", null, new { @class = "btn btn-primary" })    
    </div>

When the DeleteStudy link is clicked, the following controller method is called:

    [Authorize]
    public void DeleteStudy(string topic, string subtopic)
    {
        ...
        ...
        RedirectToAction("StudyList");
    }

The DeleteStudy method is called and executes successfully, except for the Redirect. No redirect occurs. The StudyList method (which has an Authorization attribute) is never called. Am I doing something wrong?

Upvotes: 1

Views: 798

Answers (1)

user3559349
user3559349

Reputation:

You need to change

RedirectToAction("StudyList");

to

return RedirectToAction("StudyList");

However I recommend you make your Delete action a POST rather that a GET. You don't want this added to the browser history or allow the user to enter it in the address bar. At best it's just making an unnecessary call to delete something which no longer exists, and at worst may throw an exception depending on your code

@using (Html.BeginForm("DeleteStudy", "Study", new {topic = Model.Study.PartitionKey, subtopic = Model.Study.RowKey }))
{
  @Html.AntiForgeryToken()
  <input type="submit" value="Delete Study" /> // style it to look like a link if that's what you want
}

and change the method to

[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult DeleteStudy(string topic, string subtopic)

Upvotes: 7

Related Questions