繁体   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