首页 > 解决方案 > 如何从 ASP.NET MVC 中的不同操作返回视图

问题描述

我有一个视图叫做DetailsLane另一个AddItem. 渲染时DetailsLane,它返回一个带有属性的模型类。该AddItem动作在同一个控制器中,但是当我完成了在该动作中必须做的任何事情时,我无法重新渲染DetailsLane以更新视图。

第一次调用视图:它有效!

public ActionResult DetailsLane(int? id, int? IdInstance)
{
    return View(get(IdInstance, id));
}

public ActionResult AddLine(FormCollection collection)
{
      // I did my stuff in here, and I want to return the initial View or,
      // validate something from the collection.
      return DetailsLane(val, val);     // doesn't work!
}

标签: c#asp.net-mvc

解决方案


您想使用RedirectToAction并且可以传递参数,如下所示

public ActionResult AddLine(FormCollection collection)
{
  // I did my stuff in here, and I want to return the initial View or,
  // validate something from the collection.
  return RedirectToAction("DetailsLane", new { id = val1, IdInstance = val2 });
}

请注意idIdInstance匹配您的DetailsLane操作方法中的参数名称。


推荐阅读