【问题标题】:How can I retrieve items for a sub-table based on an item ID in the master table in a Razor page?如何根据 Razor 页面的主表中的项目 ID 检索子表的项目?
【发布时间】:2018-11-29 23:35:15
【问题描述】:

我有一个 Razor 页面视图,它只是从从模型属性检索的列表中生成项目表,如下所示:

@page
@model Agency.Pages.TypeManagement.CategoryType.IndexModel

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

<h2>Index</h2>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
        <th>
            @Html.DisplayNameFor(model => model.Category[0].CategoryCode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category[0].CategoryName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category[0].CategoryDescription)
        </th>
        <th></th>
        </tr>
    </thead>
<tbody>
@foreach (var item in Model.Category) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryDescription)
        </td>
        <td>
            <a asp-page="./Edit" asp-route-id="@item.CategoryId">Edit</a> |
            <a asp-page="./Details" asp-route-id="@item.CategoryId">Details</a> |
            <a asp-page="./Delete" asp-route-id="@item.CategoryId">Delete</a>
        </td>
    </tr>
}
</tbody>
</table>

我要做的是根据主表中项目的ID,为表中的每个项目输出一个子表,如果子表中存在任何项目。我知道我可以在 Razor 语句中使用 Model.Category 等属性,但是否也可以根据当前项目的 ID 等值从视图中检索数据?

所以我想要的结果是这样的:

@foreach (var item in Model.Category) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.CategoryCode)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CategoryName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CategoryDescription)
    </td>
    <td>
        <a asp-page="./Edit" asp-route-id="@item.CategoryId">Edit</a> |
        <a asp-page="./Details" asp-route-id="@item.CategoryId">Details</a> |
        <a asp-page="./Delete" asp-route-id="@item.CategoryId">Delete</a>
    </td>
    <table>
        <tbody>
@foreach (var subitem in Model.SubCategory) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => subitem.CategoryCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => subitem.CategoryName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => subitem.CategoryDescription)
        </td>
        <td>
            <a asp-page="./Edit" asp-route-id="@subitem.CategoryId">Edit</a> |
            <a asp-page="./Details" asp-route-id="@subitem.CategoryId">Details</a> |
            <a asp-page="./Delete" asp-route-id="@subitem.CategoryId">Delete</a>
        </td>
    </tr>
}
        </tbody
    </table
</tr>
}

但是我需要根据主表当前行的item的ID来过滤SubCategory的item。

【问题讨论】:

  • 两者之间有联系吗?
  • SubCategory 有一个Category 的外键,即Category.Id
  • 过滤当前项目的子类别。 @foreach (var subitem in Model.SubCategory.Where(x =&gt; x.CategoryId = item.Id) 其中item 来自外部foreach 循环

标签: c# asp.net-core asp.net-core-2.0 razor-pages


【解决方案1】:

根据当前项目/行的标识符过滤子类别。

@foreach (var item in Model.Category) {

    //...omitted for brevity

    var subItems = Model.SubCategory.Where(x => x.CategoryId = item.Id);
    if(subItems.Any()) {
        <table>
            <tbody>
        foreach (var subitem in subItems) {
            //...omitted for brevity

其中item 来自外部foreach 循环。

参考Using the Razor Syntax: Combining Text, Markup, and Code in Code Blocks

【讨论】:

  • 谢谢。我会尝试这种方法。我不知道我可以在 Razor @if 语句中使用比简单属性更多的东西。我在哪里可以找到关于 Razor 语句中可以使用什么的文档?我找到的官方文档只显示了简单属性的使用(通常是 IList 类型)。
  • 看来这种方法有效,但是,在我的情况下,使用我作为示例提供的代码,'var' 或'if' 上不需要'@',甚至'foreach' 因为代码已经在 Razor 代码块(外部@foreach)内。至少这是代码编辑器告诉我的。我按照您的建议添加了代码,在嵌套的 Razor 代码语句中没有前导“@”,并且所有内容都编译并运行得很好。感谢@Nikosi 为我指明正确方向的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2019-09-28
  • 2021-04-19
  • 2015-04-15
  • 2021-06-11
  • 2020-05-06
相关资源
最近更新 更多