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

时间:2019-04-23 13:27:36

标签: c# asp.net asp.net-mvc

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

我尝试在控制器(2)中使用模型和make循环来在用户检查多个checbox时获取值checbox,

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

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

控制器

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; }



}

如果还有其他方法,请告诉我 谢谢!

1 个答案:

答案 0 :(得分: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()
);