在子级“post”操作后返回正确父级的“get”结果。

3

我在多个父级动作中都放置了相同的子级动作,假设有以下两个父级视图:

@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 方法?作为许多父母中的孩子,他怎么知道这一次是哪个父母调用了它?

我在谷歌上和“可能已经有答案的问题”中都找不到任何东西。

1个回答

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,执行相同的操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接