【问题标题】:How to get the .Include data in MVC C#?如何在 MVC C# 中获取 .Include 数据?
【发布时间】: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


    【解决方案1】:

    你应该像下面这样创建CafeTableViewModel

    public class CafeTableViewModel
    {
        public int TotalOrders { get; set; }
        public List<CafeTableDetails> CafeTableDetails { get; set; }
    }
    

    Indexaction中,可以通过这种方式获取

    public ActionResult Index()
    {
        var cafeTableDetails = db.CafeTableDetails.Include(c => c.CafeTable).Include(c => c.Food);
        var totalOrders = cafeTableDetails.Sum(p => p.TotalAmount);
        var viewModel = new CafeTableViewModel 
                        { TotalOrders = totalOrders, CafeTableDetails = cafeTableDetails.ToList()}
        return View(viewModel);
    }
    

    【讨论】:

    • 这能回答你的问题吗?如果您需要任何帮助,请告诉我@Steve
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2016-10-04
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多