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

时间:2017-08-16 15:16:42

标签: asp.net-mvc parent-child

我在几个父母的行动中都有同样的儿童行为,让我们说有这两个父母的观点:

@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打电话?作为许多父母的孩子,他怎么能知道哪一位父母这次称呼它?

我无法找到任何内容,无论是谷歌还是#34;可能已经有你答案的问题"。

1 个答案:

答案 0 :(得分:3)

您可以再添加一个参数来确定它的来源, 你也可以把这个参数作为一个字符串,它将成为父动作名称本身并将该参数传递给你的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

执行相同的操作