[英]how to pass list of objects from one controller to another controller?
I need to pass list of objects from one controller to another controller. 我需要将对象列表从一个控制器传递到另一个控制器。 i have read the similar questions's answers but nothing could help me.
我已经阅读了类似问题的答案,但没有任何帮助。 here is code of first controller :
这是第一个控制器的代码:
[HttpPost]
public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
{
switch (Action_Code)
{
case 1:
{ //redirecting requesting to another controller
return RedirectToAction("Quran_Loading", "Request_Handler",things);
}
default:
...
}
}
Request_Handler code : Request_Handler代码:
public class Request_HandlerController : Controller
{
public ActionResult Quran_Loading(List<thing> thin)
{...}
}
but problem is that list in Quran_Loading method is null. 但是问题是Quran_Loading方法中的列表为空。 any idea ?
任何想法 ?
Passing List from controller to another is not possible in actions, Because RedirectToAction
is HTTP request that u can't pass list to. 在操作中无法将列表从控制器传递到另一个列表,因为
RedirectToAction
是您无法将列表传递到的HTTP请求。
you can use one of three options ViewData
, ViewBag
and TempData
for passing data from controller to View or another controller 您可以使用以下三个选项之一
ViewData
, ViewBag
和TempData
将数据从控制器传递到View或另一个控制器
you can check this reference here for more information about the difference between the three options. 您可以在此处查看此参考,以获取有关这三个选项之间差异的更多信息。
[HttpPost]
public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
{
switch (Action_Code)
{
case 1:
{
TempData["Things"] = things;
// OR
ViewBag.Things = things;
// OR
ViewData["Things"] = things;
//redirecting requesting to another controller
return RedirectToAction("Quran_Loading", "Request_Handler");
}
default:
...
}
}
Request Handler 请求处理程序
public class Request_HandlerController : Controller
{
public ActionResult Quran_Loading()
{
List<thing> things = (List<thing>)TempData["Things"];
// Do some code with things here
}
}
Check this code and tell me if there is any question 检查此代码,并告诉我是否有任何问题
Look at TempData
, I think it should solve your problem. 看一下
TempData
,我认为它应该可以解决您的问题。 RedirectToAction
is HTTP request with 302 code and setting new location in headers. RedirectToAction
是具有302代码并在标头中设置新位置的HTTP请求。
ASP.NET MVC - TempData - Good or bad practice ASP.NET MVC-TempData-好的或坏的做法
[HttpPost]
public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
{
switch (Action_Code)
{
case 1:
{ //redirecting requesting to another controller
TempData["Things"]=things; //transferring list into tempdata
return RedirectToAction("Quran_Loading", "Request_Handler",things);
}
default:
...
}
}
public class Request_HandlerController : Controller
{
public ActionResult Quran_Loading()
{
List<thing> things=TempData["Things"] as List<thing>;
}
}
TempData 临时数据
Lifecycle of tempdata is very short. tempdata的生命周期非常短。 The best scenario to use when you are redirecting to another view.
重定向到另一个视图时使用的最佳方案。 Reason behind this is that redirection kill the current request and send HTTP status code 302 object to server and also it creates a new request.
这背后的原因是重定向会终止当前请求并将HTTP状态代码302对象发送到服务器,并且还会创建一个新请求。
TempData
will not be cleared after using RedirectToAction
, you should code as follow: 使用
RedirectToAction
后将不会清除TempData
,您应编写以下代码:
Controller: 控制器:
TempData["things"] = (List<thing>)things;
View: 视图:
@if(List<thing>)TempData["things"] != null)
{
<tr>
<td>
<ul>
foreach(var item in (List<thing>)TempData["things"])
{
<li><b>@item.PropertyName</b></li>
}
</ul>
</td>
</tr>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.