加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 我们可以传递模型作为参数在RedirectToAction吗

发布时间:2020-12-15 18:51:23 所属栏目:asp.Net 来源:网络整理
导读:我想知道,有任何技术,所以我们可以传递Model作为参数在RedirectToAction 例如: public class Student{ public int Id{get;set;} public string Name{get;set;}} 控制器 public class StudentController : Controller{ public ActionResult FillStudent()
我想知道,有任何技术,所以我们可以传递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();
}

方法2:使用查询字符串数据传递

或者你可以用查询字符串的帮助框架它

return RedirectToAction("GetStudent",new {Name="John",Class="clsz"});

这将生成一个GET请求

Student/GetStudent?Name=John & Class=clsz

但确保你有[HttpGet],因为RedirectToAction将发出GET请求(302)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读