我在多个父操作中放置了相同的子操作,假设有两个父视图:
@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 执行相同的操作