1

アイテムを削除する削除アクションがあります。このアイテムが削除された後、削除されたアイテムの親のアクションにリダイレクトしたい。

    // 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();
    }

どうすればこれを達成できますか?

4

2 に答える 2

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

独自の回答を作成しましたが、呼び出したいメソッドが別のコントローラーにあるようです。コントローラー名をパラメーターとして追加する必要はありません。あなたはこれを持つことができます:

// 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})
于 2013-05-07T14:06:49.943 に答える
0

ありがとう@von v.私はあなたの答えを少し修正しました、そしてそれはうまくいきます:

// Delete action of the child item
public ActionResult Delete(int id, FormCollection collection)
{
    var parent_id = queryTheParentObjectId();
    DeleteChildFromDB(id);
    return RedirectToAction("ParentAction", new { controller = "ParentController", id = parent_id });
}
于 2013-05-07T14:17:48.097 に答える