我们可以在RedirectToAction中将模型作为参数传递吗?

Ami*_*mit 58 asp.net-mvc controller asp.net-mvc-3 asp.net-mvc-4

我想知道,有任何技术,所以我们可以Model作为参数传递RedirectToAction

例如:

public class Student{
    public int Id{get;set;}
    public string Name{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

调节器

public class StudentController : Controller
{
    public ActionResult FillStudent()
    {
        return View();
    }
    [HttpPost]
    public ActionResult FillStudent(Student student1)
    {
        return RedirectToAction("GetStudent","Student",new{student=student1});
    }
    public ActionResult GetStudent(Student student)
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题 - 我可以在RedirectToAction中传递学生模型吗?

Mur*_*san 69

使用TempData

表示仅从一个请求持续到下一个请求的一组数据

[HttpPost]
public ActionResult FillStudent(Student student1)
{
    TempData["student"]= new Student();
    return RedirectToAction("GetStudent","Student");
}

[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
    Student std=(Student)TempData["student"];
    return View();
}
Run Code Online (Sandbox Code Playgroud)

替代方法 使用Query字符串传递数据

return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
Run Code Online (Sandbox Code Playgroud)

这将生成一个GET请求 Student/GetStudent?Name=John & Class=clsz

确保您要重定向到的方法被装饰,[HttpGet]因为上面的RedirectToAction将发出带有http状态代码302 Found的GET请求(执行url重定向的常用方法)

  • 我不想使用`TempData`.我知道我们可以使用TempData实现.但我的问题不同 (8认同)
  • @AmitAgrawal,简单的`NO`.检查[在ASP.NET MVC应用程序中传递数据](http://msdn.microsoft.com/en-us/library/dd394711(v = vs.100).aspx) (3认同)
  • 这是一个旧线程,但我想指出1)GetStudent操作在使用TempData时不需要有参数2)必须注意,如果要实现PRG模式,它仍然只能部分解决问题.在第二次刷新视图时,TempData将为空(按设计)! (3认同)
  • 我也知道方法2,但我的问题不同我可以实现我的问题__是/否___ (2认同)
  • 切勿使用 TempData。想象一下您有一个在天蓝色中运行的应用程序。您最终将拥有该应用程序的许多实例。如果 ARR 关联被禁用,您最终将由一个实例调用第一个方法来保存 tempData 中的数据,而第二个实例将接收第二个请求。你会拉扯你的头发来弄清楚发生了什么事。因此,请远离有状态的解决方案,例如 TempData 或会话。 (2认同)
  • @akd 大声笑吗?远离 Sessions 或 TempData?您描述的问题仅在 ARR 关联被禁用时与 Azure 有关。如果您需要*有状态* 的解决方案(几乎所有允许您登录的 Web 应用程序),那么绝对*需要 TempData 和 Sessions。 (2认同)

Vin*_*ngh 31

只需调用不需要的操作redirect to actionnew模型的关键字.

 [HttpPost]
    public ActionResult FillStudent(Student student1)
    {
        return GetStudent(student1); //this will also work
    }
    public ActionResult GetStudent(Student student)
    {
        return View(student);
    }
Run Code Online (Sandbox Code Playgroud)

  • 请注意,虽然这有效(与@hotfusion提到的更改),但仍然不符合PRG模式.如果用户点击F5,他们将再次复制原始请求! (11认同)
  • 动作GetStudent必须与模特学生一起返回View'GetStudent' - 为我工作 - **返回视图("GetStudent",学生)** (7认同)
  • 这个答案对我不起作用(页面永远不会改变),但 hotfusions 评论确实如此。 (2认同)
  • 这将显示原始请求的 url。这可能不是我们所希望的。 (2认同)

小智 11

是的,您可以传递您使用的模型

return RedirectToAction("GetStudent", "Student", student1 );
Run Code Online (Sandbox Code Playgroud)

假设student1是一个实例Student

这将产生以下网址(假设使用缺省路由的价值你student1ID=4Name="Amit")

.../Student/GetStudent/4?Name=Amit

在内部,该RedirectToAction()方法RouteValueDictionary通过使用.ToString()模型中每个属性的值来构建a .但是,绑定仅在模型中的所有属性都是简单属性时才有效,如果任何属性是复杂对象或集合则失败,因为该方法不使用递归.例如,如果Student包含属性List<string> Subjects,那么该属性将导致查询字符串值为

....&Subjects=System.Collections.Generic.List'1[System.String]

和绑定将失败,该属性将是 null