首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在控制器中使用循环时如何返回多个数据

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

Stack Overflow用户
提问于 2019-04-23 13:27:35
回答 1查看 177关注 0票数 0

当用户选中多个复选框并将数据从此视图(1)显示到另一个视图(2)时,我希望筛选数据。

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

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

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

控制器

代码语言:javascript
复制
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);
    }

视图索引

代码语言:javascript
复制
@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>
                    }

视图搜索

代码语言:javascript
复制
 @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>
        }

和基模型

代码语言:javascript
复制
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; }



}

如果有人有别的办法,请告诉我,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-23 13:56:37

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

试一试:

代码语言:javascript
复制
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()
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55812348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档