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)

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....