删除子项后,MVC4重定向到父项

Sta*_*ked 1 c# asp.net-mvc

我有一个删除项目的删除操作.删除此项后,我想重定向到已删除项的父项的操作.

    // The parent Action
    public ActionResult ParentAction(int id = 0)
    {
        Parent parent = LoadParentFromDB(id);
        return View(parent);
    }

    // Delete action of the child item
    public ActionResult Delete(int id, FormCollection collection)
    {
        DeleteChildFromDB(id);
        return RedirectToParentAction();
    }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

von*_* v. 7

使用该RedirectToAction方法并传递父对象的id

// Delete action of the child item
public ActionResult Delete(int id, FormCollection collection)
{
    var parent_id = queryTheParentObjectId();
    DeleteChildFromDB(id);
    return RedirectToAction("ParentAction", new {id=parent_id})
}
Run Code Online (Sandbox Code Playgroud)

您创建了自己的答案,似乎您要调用的方法是在另一个控制器中.您无需将控制器名称添加为参数.你可以这样:

// instead of doing this
// return RedirectToAction("ParentAction", 
//    new { controller = "ParentController", id = parent_id });
//
// you can do the following
// assuming ParentConroller is the name of your controller
// based on your own answer 
return RedirectToAction("ParentAction", "Parent", new {id=parent_id})
Run Code Online (Sandbox Code Playgroud)