简体   繁体   English

如何将对象列表从一个控制器传递到另一控制器?

[英]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 您可以使用以下三个选项之一ViewDataViewBagTempData将数据从控制器传递到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.

相关问题 如何将值从一个 controller 传递到 .netcore 中的另一个 controller - how to pass value from one controller to another controller in .netcore 如何将对象列表传递给Controller? - How to pass objects list to Controller? 如何将 Model 从 controller 传递到另一个 controller - how to pass a Model from a controller to another controller 将SelectList项目列表从一个控制器传递到另一个 - Pass SelectList item list one controller to another 如何从控制器传递对象列表以进行查看? - How do I pass a list of objects from controller to view? 将对象列表从视图传递到 controller - Pass list of objects from view to controller 如何在MVC中将XDocument从一个控制器传递到另一个 - How to pass XDocument from one controller to another in mvc 如何将大字符串从一个控制器传递到另一个控制器? - How can I pass large strings from one controller to another? 如何将AntiForgeryToken从一个控制器动作传递给另一个控制器动作? - How to pass an AntiForgeryToken from one controller action to another? 将值从一个Tabbar控制器传递到Xamarin中的另一个控制器 - Pass value from one Tabbar controller to another controller in Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM