【问题标题】:Combine detail and delete views ASP.Net MVC 5结合细节和删除视图ASP.Net MVC 5
【发布时间】:2014-10-02 15:04:10
【问题描述】:

我目前对我的详细信息和删除方法有单独的视图和控制器操作。我想将删除按钮放在详细信息视图上,这样用户就不必单击删除,然后在他们位于删除视图上时再次删除。通过没有“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 视图:

我不知道这是否有意义。

【问题讨论】:

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


    【解决方案1】:

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

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

    应该按照你的要求去做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多