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

标签 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 controller

我想知道,有什么技术可以让我们将 Model 作为 RedirectToAction 中的参数传递

例如:

public class Student{
    public int Id{get;set;}
    public string Name{get;set;}
}

Controller

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

Represents a set of data that persists only from one request to the next

[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();
}

替代方式 使用查询字符串传递数据

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

这将生成一个 GET 请求,例如 Student/GetStudent?Name=John & Class=clsz

Ensure the method you want to redirect to is decorated with [HttpGet] as the above RedirectToAction will issue GET Request with http status code 302 Found (common way of performing url redirect)

关于asp.net-mvc - 我们可以在 RedirectToAction 中将模型作为参数传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22505674/

相关文章:

asp.net-mvc - 如何使用ajax将DatePicker创建的日期值传递给mvc 5 Controller

asp.net-mvc - StackExchange API 如何实现通用包装器对象?

javascript - 在 asp.net 中捆绑

asp.net-mvc - 已为布局页面定义部分但未呈现 "~/Views/Shared/_Layout.cshtml": "head"

.net - 如何使用主干单页应用程序在 MVC4 中测试 AntiForgeryToken

c# - 将 asp.net core 从 .net 5 移植到 .net 6 时首页加载缓慢

intellisense - ASP.NET MVC 3 - ViewBag 属性不提供 IntelliSense

asp.net-mvc-3 - 如何实现 MVC3 模型 URL 验证?

asp.net - 无法使用 JQGrid 添加行

javascript - jquery 数据表和 css 未在 mvc 4 中呈现