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重定向的常用方法)
Vin*_*ngh 31
只需调用不需要的操作redirect to action
或new
模型的关键字.
[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)
小智 11
是的,您可以传递您使用的模型
return RedirectToAction("GetStudent", "Student", student1 );
Run Code Online (Sandbox Code Playgroud)
假设student1
是一个实例Student
这将产生以下网址(假设使用缺省路由的价值你student1
是ID=4
和Name="Amit"
)
.../Student/GetStudent/4?Name=Amit
在内部,该RedirectToAction()
方法RouteValueDictionary
通过使用.ToString()
模型中每个属性的值来构建a .但是,绑定仅在模型中的所有属性都是简单属性时才有效,如果任何属性是复杂对象或集合则失败,因为该方法不使用递归.例如,如果Student
包含属性List<string> Subjects
,那么该属性将导致查询字符串值为
....&Subjects=System.Collections.Generic.List'1[System.String]
和绑定将失败,该属性将是 null
归档时间: |
|
查看次数: |
89451 次 |
最近记录: |