【发布时间】:2020-01-10 00:34:49
【问题描述】:
我有这个将 IQueryable 返回到视图的操作方法。
public ActionResult Index()
{
var cafeTableDetails = db.CafeTableDetails.Include(c => c.CafeTable).Include(c => c.Food);
return View(cafeTableDetails.ToList());
}
在我看来,
@model IEnumerable<MVC_Cafe.Models.CafeTableDetails>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CafeTable.TableNo)
</th>
<th>
@Html.DisplayNameFor(model => model.Food.FoodName)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.TotalAmount)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CafeTable.TableNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Food.FoodName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
<tr>
<td colspan="5"></td>
</tr>
</table>
我可以显示视图,但现在我不知道如何在最后一个 tr 标记中的 for each 循环之后显示总顺序。总数存储在CafeTable 中。这是我的模型
public class CafeTable
{
public int Id { get; set; }
public string TableNo { get; set; }
public TableStatus TableStatus { get; set; }
public decimal TableAmount { get; set; }
public int TotalOrders { get; set; }
// Navigation Properties
public ICollection<CafeTableDetails> CafeTableDetails { get; set; }
}
public enum TableStatus
{
Empty, Occupied
}
public class CafeTableDetails
{
public int Id { get; set; }
public int CafeTableId { get; set; } // FK
public int FoodId { get; set; } // FK
public int Quantity { get; set; }
public decimal TotalAmount { get; set; }
// Navigation Properties
public CafeTable CafeTable { get; set; }
public Food Food { get; set; }
}
我可以在 for each 循环中进行此操作,但不确定如何将其添加到 outside for each 循环中
@Html.DisplayFor(modelItem => item.CafeTable.TableAmount)
【问题讨论】:
标签: c# asp.net-mvc linq view