如何在控制器中使用循环时返回多个数据

问题描述 投票:0回答:1

我想在用户选中多个复选框时过滤数据,并将此视图(1)中的数据显示到另一个视图(2)

当用户选中多个复选框时,我尝试在Controller(2)中使用Model和make循环来获取值复选框,

返回时我在控制器中出现循环问题...当用户选中多个复选框时,我无法返回多个数据。

它只返回最后一个循环的数据

调节器

ContainerTrackingSystemEntities db = new ContainerTrackingSystemEntities();
    BaseModel modelResult = new BaseModel();
    BaseModel model = new BaseModel();
    // GET: CurrentContainerReport
    public ActionResult Index()
    {
        //if (Session["User"] == null)
        //{
        //    SetAlert("Vui lòng đăng nhập", "info");
        //    return RedirectToAction("LoginUsers", "Users");
        //}
        ViewBag.PortCode = db.APS_Inventory_Port.OrderBy(n => n.Code);


        //model.aPS_Inventory_Containers = (from c in db.APS_Inventory_Container select c).GroupBy(n => n.Size).Select(n => n.FirstOrDefault()).Distinct().ToList();
        model.aPS_Inventory_OwnerAgentss = (from o in db.APS_Inventory_OwnerAgent select o).OrderBy(n => n.Code).ToList();
        model.aPS_Inventory_Portss = (from p in db.APS_Inventory_Port select p).OrderBy(n => n.Code).ToList();
        model.aPS_Inventory_Containerss = (from c in db.APS_Inventory_Container select c).GroupBy(n => n.Size).Select(n => n.FirstOrDefault()).OrderBy(n => n.Size).ToList();




        return View(model);
    }

    [HttpPost]
    public ActionResult Index(BaseModel model)
    {

        TempData["model"] = model;

        return RedirectToAction("Search");
    }


    [HttpGet]
    public ActionResult Search(BaseModel model)
    {
         model = (BaseModel) TempData["model"];

        List<APS_Inventory_Container> aPS_Inventory_Containers = new List<APS_Inventory_Container>();

        for (int i = 0; i < model.aPS_Inventory_OwnerAgentss.Where(n => n.IsCheckedOwnerAgent == true).Count(); i++)
        {
            bool isChecked = model.aPS_Inventory_OwnerAgentss[i].IsCheckedOwnerAgent;
            Guid? OwnerID = model.aPS_Inventory_OwnerAgentss[i].OwnerAgentID;

            aPS_Inventory_Containers = (from c in db.APS_Inventory_Container
                    where c.PortCode == model.aps_inventory_port.Code && c.OwnerAgentID == OwnerID
                    select c).ToList();


        }    
        return View(aPS_Inventory_Containers);
    }

查看索引

@for (int i = 0; i < Model.aPS_Inventory_OwnerAgentss.Count(); i++)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(item => item.aPS_Inventory_OwnerAgentss[i].Code)
                            </td>
                            <td>
                                @Html.CheckBoxFor(item => item.aPS_Inventory_OwnerAgentss[i].IsCheckedOwnerAgent, new { @checked = "checked" })
                                @Html.HiddenFor(item => item.aPS_Inventory_OwnerAgentss[i].OwnerAgentID)
                                @Html.HiddenFor(item => item.aPS_Inventory_OwnerAgentss[i].Code)
                            </td>
                        </tr>
                    }

查看搜索

 @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(itemSelect => item.Code)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.Status)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.PortCode)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.Size)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.Type)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.DateofManufactured)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.OwnerAgentID)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.Comments)
                </td>
                <td>
                    @Html.DisplayFor(itemSelect => item.approve)
                </td>

            </tr>
        }

和基础模型

public class BaseModel
{
    public APS_Inventory_Bill aps_inventory_bill { get; set; }
    public APS_Inventory_BillDetails aps_inventory_billdetails { get; set; }
    public APS_Inventory_Container aps_inventory_container { get; set; }
    public APS_Inventory_Depot aps_inventory_depot { get; set; }
    public APS_Inventory_Port aps_inventory_port { get; set; }
    public APS_Inventory_OwnerAgent aps_inventory_owneragent { get; set; }


    public List<APS_Inventory_Container> aPS_Inventory_Containerss { get; set; }
    public List<APS_Inventory_OwnerAgent> aPS_Inventory_OwnerAgentss { get; set; }
    public List<APS_Inventory_Port> aPS_Inventory_Portss { get; set; }
    public List<APS_Inventory_Bill> aPS_Inventory_Billss { get; set; }
    public List<APS_Inventory_BillDetails> aPS_Inventory_BillDetailss { get; set; }



}

如果有人有其他方式,请让我知道谢谢!

c# asp.net asp.net-mvc
1个回答
0
投票

使用行aPS_Inventory_Containers = ....,每次运行循环时都会覆盖aPS_Inventory_Containers的值。换句话说,您使用新值替换其先前的值。所以在循环结束后,只剩下最后一个值 - 你已经销毁了所有其他值。您可能希望将项添加到列表中,而不是每次都替换整个列表。

试试这个:

aPS_Inventory_Containers.AddRange( 
  (from c in db.APS_Inventory_Container
   where c.PortCode == model.aps_inventory_port.Code && c.OwnerAgentID == OwnerID
   select c).ToList()
);
© www.soinside.com 2019 - 2024. All rights reserved.