簡體   English   中英

在 RazorPages asp.net core 2_1 中顯示 parentcategoryname

[英]show parentcategoryname in RazorPages asp.net core 2_1

這是我的類別類映射到我的數據庫


            public int CategoryID { get; set; }
        public string CategoryName { get; set; }

        [ForeignKey("category")]
        public int? ParentCategoryID { get; set; }

        public Category category { get; set; }

        public List<Category> Categories { get; set; }

這是我的剃刀觀點


        <div class="row">
    <div class="col-md-12">
        <div class="card bg-dark">
            <div class="card-header bg-light">
                <h2>Categories List</h2>
                <a asp-page="./Create"><span class="text-primary ml-2">Create New</span><i class="fa fa-plus-circle text-primary"></i></a>
            </div>
            <div class="card-body bg-light">
                <form method="post">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Category Name
                                </th>
                                <th class="text-center">
                                    Parent CategoryName
                                </th>
                                <th class="text-center">
                                    Operation
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Categories)
                            {
                                <tr>
                                    <td>
                                        @item.CategoryName
                                    </td>
                                    <td class="text-center">
                                        @if (item.ParentCategoryID != null)
                                        {
                                            <a asp-page="./SubCategories" asp-route-id="@item.CategoryID" class="btn btn-secondary">@father.FatherName(item.CategoryID)</a>
                                        }
                                        else
                                        {
                                            <span><i class="fa fa-check-square text-success"></i></span>
                                        }

                                    </td>
                                    <td class="text-center">
                                        <a asp-page="./Edit" asp-route-id="@item.CategoryID"><i class="fa fa-edit text-success"></i></a> |
                                        <button asp-page-handler="Delete" asp-route-id="@item.CategoryID"><i class="fa fa-trash text-danger"></i></button> |
                                        <a asp-page="./Details" asp-route-id="@item.CategoryID"><i class="fa fa-eye text-primary"></i></a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>

我不知道如何顯示父類別名稱的名稱。 我可以在 mvc 中做到這一點,但我不能在 razor pages 中做到這一點。 甚至不查詢大量工作。

我的一些疑問:

        var faTher = (from c in _unitOfWork._Context.Categories
                          where (c.ParentCategoryID == id)
                          select new
                          {
                              Name = c.CategoryName
                          }).ToString();
        string faTher = _unitOfWork._Context.Categories.Where(c => c.ParentCategoryID == id).Select(a => 
        a.CategoryName).FirstOrDefault();   

我很困惑,我不知道我的問題到底在哪里。 其實我想把主類的名字作為字符串返回,這樣我就可以顯示了

您的意思是要使用 childCategoryId 或 ParentCategoryId 獲取 ParentCategoryName 嗎?

如果您想通過 childCategoryID 獲取 parentCategoryName,您可以這樣做:

//get ParentCategoryID by childCategoryID
    var ParentCategoryID=_unitOfWork._Context.Categories.Where(c=>c.CategoryID==childCategoryID).FirstOrDefault().CategoryID;
//get PatentCategoryName by ParentCategoryID 
    var PatentCategoryName=_unitOfWork._Context.Categories.Where(c=>c.CategoryID==ParentCategoryID).FirstOrDefault().CategoryName;

如果你想通過 ParentCategoryID 獲取 parentCategoryName,你可以這樣做:

//get PatentCategoryName by ParentCategoryID 
        var PatentCategoryName=_unitOfWork._Context.Categories.Where(c=>c.CategoryID==ParentCategoryID).FirstOrDefault().CategoryName;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM