在 Razor 视图中显示列表

尼克弗利特伍德

我的目标是组合两个表并将它们显示在剃刀视图的列表中。在一些帮助下,我能够构建一个拉取数据的 linq 查询以及一个 viewModel。

我在这里有 linq 查询

public IActionResult AvailableJobIndex()
    {
        var today = DateTime.Now.Date;
        
        var jobs =
           from h in _context.PostThrs
           join e in _context.PostEigs on h.ThrZero equals e.EigZero
           where h.ThrDate > today && h.ThrText == "SERVICE DATE"
              && e.EigAgen == "OPEN"
           select new AgentClientIndexVM
           {
               Zero = h.ThrZero,
               ThrDate = h.ThrDate,
               ThrTime = h.ThrTime,
               ThrText = h.ThrText,
               EigAgen = e.EigAgen,
               EigRole = e.EigRole,
               EigLoad = e.EigLoad,
               EigNote = e.EigNote
           };


        return View(jobs.ToList());
    }

下面是我构建的视图模型。

public class AgentClientIndexVM
{
    public string Zero { get; set; }

    public DateTime ThrDate { get; set; }

    public string ThrTime { get; set; }

    public string ThrText { get; set; }

    public string EigAgen { get; set; }

    public string EigRole { get; set; }

    public decimal EigLoad { get; set; }

    public string EigNote { get; set; }
}

我还没有把工作视图放在一起。当我尝试使用 foreach 语句时,我的问题就出现了。视图模型不携带列表。请告知如何在视图中显示列表。谢谢

更新:

这是我的看法。我用“……”把它剪下来

@model AURA.ViewModels.AgentClientIndexVM 

@{
    ViewData["Title"] = "AvailableJobIndex";
}

<h1>Available Job Index</h1>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Zero)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ThrDate)
            </th>
            ...
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayNameFor(model => model.Zero)
                </td>
                <td>
                    @Html.DisplayNameFor(model => model.ThrDate)
                </td>
                ...
                <td>
                    <a asp-action="PostDetail" asp-route-zero="@item.Zero">Details</a>
                </td>
            </tr>
        }
    </tbody>
</table>

更新添加了另一个虚拟机

public class AvailableJobListVM
{
    public List<AgentClientIndexVM> JobList { get; set; }
}
林斯特罗姆

如果您创建另一个模型并创建一个 type 属性,List<AgentClientIndexVM>那么您可以在尝试时进行枚举。

public class MyPageModel {
    public List<AgentClientIndexVM> MyList {get; set;}
}

MyPageModel model = new List<AgentClientIndexVM>();
model.MyList = from h in _context.PostThrs
               join e in _context.PostEigs on h.ThrZero equals e.EigZero
               where h.ThrDate > today && h.ThrText == "SERVICE DATE" && e.EigAgen == "OPEN"
               select new AgentClientIndexVM
               {
                   Zero = h.ThrZero,
                   ThrDate = h.ThrDate,
                   ThrTime = h.ThrTime,
                   ThrText = h.ThrText,
                   EigAgen = e.EigAgen,
                   EigRole = e.EigRole,
                   EigLoad = e.EigLoad,
                   EigNote = e.EigNote
               }.ToList();

        return View(model);

@model AURA.ViewModels.MyPageModel

现在你可以使用 foreach

foreach(var element in Model.MyList) {
    statements
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章