简体   繁体   English

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

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

this is my category class map to my database这是我的类别类映射到我的数据库


            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; }

and this is my razor view这是我的剃刀观点


        <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>

i dont know how to show the name of parent category name .我不知道如何显示父类别名称的名称。 i can do this in mvc but i couldn't do this in razor pages .我可以在 mvc 中做到这一点,但我不能在 razor pages 中做到这一点。 not even queries dosent work .甚至不查询大量工作。

some of my queries :我的一些疑问:

        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();   

I'm confused and I do not know where my problem really is.我很困惑,我不知道我的问题到底在哪里。 In fact, I want the name of the main category to be returned as a string so that I can display it其实我想把主类的名字作为字符串返回,这样我就可以显示了

Do you mean you want to get ParentCategoryName with childCategoryId or with ParentCategoryId?您的意思是要使用 childCategoryId 或 ParentCategoryId 获取 ParentCategoryName 吗?

If you want to get parentCategoryName by childCategoryID,you can do like this:如果您想通过 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;

If you want to get parentCategoryName by ParentCategoryID,you can do like this:如果你想通过 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