我有一个MVC5应用程序,它有几个控制器,脚手架上有EF6 CRUD操作和相关视图。这些控制器/视图集之一用于管理病人标识符表,并且在编辑或删除完成后,控制器按预期返回到标识符索引视图的操作链接。
但是,患者标识符也显示在患者控制器的各种视图上,并且从Patient.Edit视图中,我对标识符控制器的编辑或删除操作进行了Html.ActionLink调用。当从Patient.Edit视图调用后者时,我希望它们在完成时返回到该视图。
我有什么办法能做到这一点吗?
发布于 2014-08-22 15:54:24
在我的一个项目中,我就是这样做的:
public ActionResult Edit(int id, string returnUrl)
{
    // find the model (code not shown)
    return View(model);
}在Edit视图中,您不需要做任何特殊的事情,在“后操作”中
[HttpPost]
public ActionResult Edit(Model model)
{
    if (ModelState.IsValid)
    {
        // save Model...    
        return Redirect(Request.Params["returnUrl"]);
        // Request.Query will work as well since it is in the querystring
        // of course you should check and validate it as well...
    }
    // else return the View as usual, not shown
}要使用它,在从页面创建“编辑”链接时,只需指定额外的returnUrl参数:
@Html.ActionLink("Edit", "Edit",
    new { controller = "YourController", 
          returnUrl = Url.Action("Index", "ThisController",) 
        })希望能帮上忙。
https://stackoverflow.com/questions/25450178
复制相似问题