2014-10-02 37 views
0

我目前有我的細節和刪除方法單獨的意見和控制器操作。我想將刪除按鈕放在詳細信息視圖中,以便用戶不必單擊刪除,然後再次刪除它們在刪除視圖上。我有這樣的大部分的方式,通過不具有「獲取」刪除方法並使用ajax.actionlink助手裏的細節視圖調用POST方法:結合細節和刪除視圖ASP.Net MVC 5

@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: /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!"); 
    } 
} 

不幸的是,它返回視圖內當前的詳細信息的查看:

enter image description here

我不知道這使得是否感覺。

回答

1

當你的請求是基於AJAX,您需要返回JavaScript來執行重定向 - 是這樣的:

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

應該做你是問。

+0

是的!!!謝謝,這很好用! – steveareeno 2014-10-02 17:06:01