我有2个已填充的ILists供应商和具有共同财产的VwSrmAhmSupplier.SupplierNo和VwSrmAhmSupplier.AhmSupplierNo。我想将它们在该公共属性上组合为一个IList,在该IList中仍可以访问各个类的属性,以便可以在View中有效地访问它们。
因此,如果我拉了CommonList中的第一项,然后询问CommonList(1).Supplier.SupplierNo和CommonList(1).VwAhmSrmSupplier.AhmSupplierNo-这两个字段将是相同的。
也许我有这样的课程:
public class SupplierDetail
{
public Supplier Supplier { get; set; }
public Models.ExternalData.VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
}
public IList<Supplier> Suppliers { get;set; }
public IList<Models.ExternalData.VwSrmAhmSupplier> VwSrmAhmSuppliers { get; set; }
public IList<SupplierDetail> SupplierDetails;
public async Task OnGetAsync(Boolean? All)
{
//don't show all records unless explicity asked to!
if (All == true)
{
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
var supplierNos = Suppliers.Select(s => s.SupplierNo).ToList();
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(v => supplierNos
.Any(s => s == v.AhmSupplierNo))
.ToListAsync();
SupplierDetails = ??
}
我尝试生成的HTML表将如下所示:
<tbody>
@foreach (var item in Model.CommonList)
{
<tr>
<td>
<a asp-page="./Details" asp-route-id="@item.Id">@Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.CreateDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Creator)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Category.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Status.Description)
</td>
<td>
<a asp-page="./Delete" asp-route-id="@item.Id" class="btn btn-danger">Hide</a>
</td>
</tr>
}
</tbody>
感谢@Selvin在引导我走正确道路上的帮助。
创建2个类的复合类
public class SupplierDetail
{
public Supplier Supplier { get; set; }
public VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
}
然后,通过循环创建类:
SupplierDetails = new List<SupplierDetail>();
foreach (Supplier supplier in Suppliers)
{
SupplierDetail supplierDetail = new SupplierDetail
{
Supplier = supplier,
VwSrmAhmSupplier = VwSrmAhmSuppliers.Where(s => s.AhmSupplierNo == supplier.SupplierNo).First()
};
SupplierDetails.Add(supplierDetail);
}
最后,在视图中访问您的对象属性
<table id="table" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.SupplierNo)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].VwSrmAhmSupplier.AhmSupplierNm)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.CreateDate)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Creator)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.SupplierDetails)
{
<tr>
<td>
<a asp-page="./Details" asp-route-id="@item.Supplier.Id">@Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.CreateDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Creator)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Category.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Status.Description)
</td>
<td>
<a asp-page="./Delete" asp-route-id="@item.Supplier.Id" class="btn btn-danger">Hide</a>
</td>
</tr>
}
</tbody>
</table>