Asp.net mvc 4 RedirectToAction()后页面中的Html.RenderAction失败

Asp.net mvc 4 RedirectToAction()后页面中的Html.RenderAction失败,asp.net-mvc-4,Asp.net Mvc 4,我有一个页面“ViewUser”。用这种结构 //display something about the user @{Html.RenderAction("UpdateUser", new { UserId = @Model.UserId });} //display some more stuff 在UpdateUser的控制器方法中,我有 public ActionResult UpdateUser(int UserId){//populate form vars [HttpPost]

我有一个页面“ViewUser”。用这种结构

//display something about the user
@{Html.RenderAction("UpdateUser", new { UserId = @Model.UserId });}
//display some more stuff
在UpdateUser的控制器方法中,我有

public ActionResult UpdateUser(int UserId){//populate form vars
[HttpPost]
public ActionResult UpdateUser(User u)//
// code to update the user
return RedirectToAction("ViewUser", new { User = u.UserId });
当我打开页面时,它工作正常,我在UpdateUser表单中看到了我的用户数据和日期。 但是,当我更新用户数据并从UpdateUser重定向到ViewUser页面时,我得到 “不允许子操作执行重定向操作。”

但是再次手动打开页面表明更新成功。
据我所知,浏览器应该对所有内容执行新的GET,因此我不明白为什么它可以用于手动刷新,但在UpdateUser返回重定向时不起作用?

错误很明显:

Child actions are not allowed to perform redirect actions.
当您直接调用某个操作(而不是作为子操作)时,它负责决定返回到浏览器的内容。因此它可以决定返回重定向

但是,如果此操作作为子操作调用,则已经有一个主操作决定返回浏览器的内容,并要求此子操作呈现其他HTML。因此,如果子操作返回不同的内容(作为重定向),它将失败

看到这个了吗

调用子操作方法并在父视图中内联呈现结果

重定向或任何非HTML的内容(视图/PartialView结果)不能在父视图中呈现

您可以模拟来自子操作的“重定向”,包括重定向页面的脚本,方法是在其上包含以下JavaScript():


无论如何,您更愿意从主视图执行此操作,方法是使用与您在partial上使用的相同逻辑来决定进行重定向。

谢谢JotaBe。那么,如何将ViewUser作为非子操作返回?这个图案有什么问题吗?我觉得很自然?
window.location.replace('@Url.Action(...)');