我想知道,有任何技术,所以我们可以传递Model作为参数在RedirectToAction
例如:
public class Student{ public int Id{get;set;} public string Name{get;set;} }
控制器
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(); } }
我的问题 – 我可以传递学生模型在RedirectToAction吗?
解决方法
您可以使用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(); }
或者你可以用查询字符串的帮助框架它
return RedirectToAction("GetStudent",new {Name="John",Class="clsz"});
这将生成一个GET请求
Student/GetStudent?Name=John & Class=clsz
但确保你有[HttpGet],因为RedirectToAction将发出GET请求(302)