MVC RedirectToAction无法按预期工作

时间:2015-08-02 23:50:23

标签: asp.net-mvc

我的网页包含以下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>

单击DeleteStudy链接时,将调用以下控制器方法:

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

调用DeleteStudy方法并成功执行,但重定向除外。没有重定向。从不调用StudyList方法(具有Authorization属性)。我做错了吗?

1 个答案:

答案 0 :(得分:7)

您需要更改

RedirectToAction("StudyList");

return RedirectToAction("StudyList");

但是我建议您将删除操作设为POST而不是GET。您不希望将此添加到浏览器历史记录中或允许用户在地址栏中输入它。最好只是做一个不必要的调用来删除不再存在的东西,最坏的情况可能会根据你的代码抛出异常

@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
}

并将方法更改为

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