Asp.net-Mvc
我們可以在 RedirectToAction 中將模型作為參數傳遞嗎?
我想知道,有什麼技術可以
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 中傳遞學生模型嗎?
使用臨時數據
表示僅從一個請求持續到下一個請求的一組數據
[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","Student", new {Name="John", Class="clsz"});這將生成一個 GET 請求,例如
Student/GetStudent?Name=John & Class=clsz確保您要重定向到的方法被裝飾,
[HttpGet]因為上面的 RedirectToAction 將發出帶有 http 狀態程式碼 302 Found 的 GET 請求(執行 url 重定向的常用方法)