[英]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.