简体   繁体   English

如何将模型数据传递给一个控制器到另一个控制器

[英]How to pass model data to one controller to another controller

pass model data to one controller to another controller is possible?可以将模型数据传递给一个控制器到另一个控制器吗?

I want to passe the model data to one controller ro another controller.我想将模型数据传递给一个控制器和另一个控制器。

[HttpPost]
        public ActionResult Personal(StudentModel student)
        {                          
                return RedirectToAction("nextStep", new { model = student});          
        }

        public ActionResult nextStep(StudentModel model)
        {           
            return View(model);
        }

in nextStep controller model value is null.nextStep控制器模型值为空。 How to do it?怎么做? I need to StudentModel data in nextStep conreoller.我需要在 nextStep 控制器中使用 StudentModel 数据。

You are using RedirectToAction .您正在使用RedirectToAction It will issue GET request.它将发出 GET 请求。 There are two ways you can pass your model here.您可以通过两种方式在此处传递模型。

1. TempData 1. 临时数据

You need to persist the model in TempData and make RedirectToAction .您需要将模型保留在 TempData 中并进行RedirectToAction However the restriction is it will be available only for the immediate request.然而,限制是它只能用于即时请求。 In your case, it is not a problem.在你的情况下,这不是问题。 You can do it with TempData你可以用TempData做到这TempData

public ActionResult Personal(StudentModel student)
{                          
       TempData["student"] = student;
       return RedirectToAction("nextStep", "ControllerName");          
}

public ActionResult nextStep()
{      
       StudentModel model= (StudentModel) TempData["student"];
       return View(model);
}

2. Passing in a query string 2. 传入查询字符串

As the request is GET, we can pass the data as Query string with the model property name.由于请求是 GET,我们可以将数据作为带有模型属性名称的查询字符串传递。 MVC model binder will resolve the query string and convert it as model. MVC 模型绑定器将解析查询字符串并将其转换为模型。

 return RedirectToAction("nextStep", new { Name = model.Name, Age=model.Age });

Also take a note passing sensible data in a Query string is not advisable .还要注意在查询字符串中传递合理的数据是不可取的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Model 从 controller 传递到另一个 controller - how to pass a Model from a controller to another controller 如何将模型传递给另一个区域的控制器? - How to pass a model to a controller on another area? 如何将值从一个 controller 传递到 .netcore 中的另一个 controller - how to pass value from one controller to another controller in .netcore 如何将对象列表从一个控制器传递到另一控制器? - how to pass list of objects from one controller to another controller? 将模型从一个动作传递到同一个控制器中的另一个动作 - pass model from one action to another action in same controller 在将一个控制器传递给另一控制器时遇到问题 - Getting issue to pass one controller to another controller 将创建的数据传递给另一个控制器 - Pass created data to another controller 如何将数据从数据库从一个控制器传递到在ASP.NET Core中拥有自己的数据库的另一个控制器? - How do I pass data from a database from one controller to another controller that has its own database in ASP.NET Core? 如何将模型传递给WebApi控制器? - How to pass model to WebApi controller? 如何将易碎数据(例如,连接字符串)从一个控制器传递到另一个 - How to pass fragile data e.g connection string from one controller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM