c# - 在 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; }
}
解决方案
如果您创建另一个模型并创建类型的属性,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
}
推荐阅读
- multithreading - SWI-Prolog:使用带有互斥锁的线程
- r - R中的循环相关测试
- reactjs - 如何在 React 中使用 gorilla/csrf
- erlang - yaws 可以在嵌入式模式下使用,而不是放在relx 或included_applications 中吗?
- google-sheets - 为什么 sumif 不添加行值?
- wordpress - 如何在 Wordpress 中通过单页上传视频?
- excel - 想用一个变量分割列而不用用例
- java - 将用户输入从主方法调用到另一个方法
- flutter - 颤振控制台和 C:users\Dell 的问题
- r - 如何在带有 R 的矩阵中使用 for 循环代码