【问题标题】:Loop an array and send the result to view ASP.NET循环数组并将结果发送到查看 ASP.NET
【发布时间】:2021-10-24 01:46:00
【问题描述】:

我有存储在数组中的数据库检索数据。该数组包含一些 JobID。我想遍历这些 JobIds 并从数据库中找到匹配项并将其显示在视图中。数组中的 JobId 可以超过 1 个。

到目前为止我做了什么:

public ActionResult Index()
{
    string[] arr = GetAgentJobId();
    int[] ids = arr.Select(int.Parse).ToArray();
    List<Requisition> Requisitions = _context.Requisitions.ToList();
    for (int i = 0; i <= ids.Count(); i++)
    {
        var req = from r in Requisitions
                  orderby r.JobID descending
                  where r.Status == "Approved" && r.JobID == ids[i]
                  select r;
        
            return View(req);
    }
    return View();
}

我将 return View(reg) 放入循环中。我认为问题是因为i++ 无法访问。我怎样才能返回这个View(req)

【问题讨论】:

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


    【解决方案1】:

    一个动作方法的片段:

        var list = arr.Select(int.Parse).ToList();
        var model = _context.Requisitions.Where(e => list.Contains(e.JobID) && e.Status == "Approved").OrderByDescending(r => r.JobID).ToList();
        return View(model);
    

    并且视图中的模型可能被定义为@model IEnumerable&lt;Requisition&gt;@model IList&lt;Requisition&gt;

    【讨论】:

      【解决方案2】:

      看起来它将在第一场比赛中返回。删除内部返回,将 req 添加到新数组,然后循环遍历整个 JobIds 列表。当所有 JobId 都匹配后,将新数组返回到视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 2013-11-14
        • 2016-11-18
        • 1970-01-01
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多