我想知道,有什么技术可以让我们将 Model
作为 RedirectToAction
中的参数传递
例如:
public class Student{
public int Id{get;set;}
public string Name{get;set;}
}
Controller
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
Represents a set of data that persists only from one request to the next
[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
Ensure the method you want to redirect to is decorated with
[HttpGet]
as the above RedirectToAction will issue GET Request with http status code 302 Found (common way of performing url redirect)
关于asp.net-mvc - 我们可以在 RedirectToAction 中将模型作为参数传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22505674/