结合细节和删除视图ASP.Net MVC 5

时间:2014-10-02 15:04:10

标签: c# asp.net-mvc controller actionresult

我目前对我的详细信息和删除方法有单独的视图和控制器操作。我想在详细信息视图中放置删除按钮,这样用户就不必单击删除,然后再次删除它们在删除视图上。我大部分时间都没有这样做" get"删除方法并在详细信息视图中使用ajax.actionlink帮助程序来调用post方法:

    @Ajax.ActionLink("Delete", "Delete", 
    new { id = Model.DepartmentId }, 
    new AjaxOptions { HttpMethod="POST", UpdateTargetId="output", Confirm= "Are you sure you want to delete this item?" }, 
    new { @class = "btn btn-danger" })

唯一的问题是当删除成功时,我想重定向到搜索视图。目前,我的删除控制器" post"方法如下:

//
// POST: /Department/Delete/5
[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult Delete(DepartmentViewModel vmNotUsed, int id = 0)
{
    if (id != 0)
    {
        // check to see if the department item is associated with an asset assignment
        bool InUseByAssetAssignment = AssetAssignmentService.ValueInUse(x => x.DepartmentId == id);

        if (InUseByAssetAssignment == false)
        {
            DepartmentService.DeleteDepartment(id);
            return RedirectToAction("Search");
        }
        else
        {
            return Content("<p style='color:#f00';>This department cannot be deleted because there are items associated with it.</p>");
        }
    }
    else
    {
        return Content("You must select a Department to delete!");
    }
}

不幸的是,它返回当前详细信息视图的视图INSIDE:

enter image description here

我不知道这是否有意义。

1 个答案:

答案 0 :(得分:2)

由于您的请求是基于AJAX的,您需要返回javascript来执行重定向 - 例如:

return JavaScript(string.format("window.location = '{0}'", Url.Action("Search")));

应该按照你的要求做。