我们可以通过模型作为RedirectToAction参数? [英] Can we pass model as a parameter in RedirectToAction?
本文介绍了我们可以通过模型作为RedirectToAction参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,有什么技术,所以我们可以通过模式
作为一个参数 RedirectToAction
I want to know, there is any technique so we can pass Model
as a parameter in 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 code>
[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();
}
方法二:使用传递查询字符串数据
Approach 2: using Query string data passed
或者你可以用查询字符串的帮助像
Or you can frame it with the help of query strings like
return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
这将产生一个GET请求像
This will generate a GET Request like
Student/GetStudent?Name=John & Class=clsz
但要确保你有 [HTTPGET]
,因为 RedirectToAction
将发行 GET请求( 302)
这篇关于我们可以通过模型作为RedirectToAction参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文