微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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吗?

解决方法

您可以使用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)

原文地址:https://www.jb51.cc/aspnet/254009.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐