簡體   English   中英

在孩子`post`動作之后返回正確的父母的'get`

[英]Return the right parent's `get` after a child `post` action

我在幾個父母的動作中都有相同的子動作,讓我們說有兩個父視圖:

@model Parent1Model 

@{
ViewBag.Title = "Parent1";
Layout = "~/Views/Shared/_Layout.cshtml";

}
@Html.Action("Child","MainControler")

第二個父母:

@model Parent2Model

@{
ViewBag.Title = "Parent2";
Layout = "~/Views/Shared/_Layout.cshtml";

}
@Html.Action("Child","MainControler")

所有操作都放在同一個控制器中。

public ActionResult parent1() //GET
{
    Parent1Model model = new Parent1Model()
    return View(model );
}
public ActionResult parent2() //GET
{
    Parent2Model model = new Parent2Model()
    return View(model );
}
public ActionResult Child() //GET
{
    ChildModel model = new ChildModel()
    return View(model );
}

[HttpPost]
public ActionResult Child(ChildModel model) //POST
{
    DoThingsWithResult(model)

    // The next line should return the get of the parent action

    return RedirectToParent() // This does not exist...
}

現在,我該如何告訴孩子給他父母的GET打電話? 作為許多父母的孩子,他怎么能知道哪一位家長這次稱呼它?

我找不到任何東西,谷歌和“可能已經有你的答案的問題”。

您可以添加一個參數來確定它的來源,您也可以將此參數設置為一個字符串,該字符串將成為父操作名稱本身並將該參數傳遞給RedirectToAction(param)

防爆

[HttpPost]
public ActionResult Child(string ParentName, ChildModel model) //POST
{
    DoThingsWithResult(model)

    // The next line should return the get of the parent action

    return RedirectToAction(ParentName) // This does not exist...
}

在您的父動作中

public ActionResult parent2() //GET
{
    Parent2Model model = new Parent2Model();
    ViewBag.Parent = "parent2";
    return View(model );
}

在你看來

@model Parent2Model

@{
ViewBag.Title = "Parent2";
Layout = "~/Views/Shared/_Layout.cshtml";

}
@Html.Action("Child","MainControler", new {ParentName= ViewBag.Parent})

為父母行動1做同樣的事情

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM