在我的 RazorPage 表单中生成类别和子类别

Generating Categories and Subcategories in my RazorPage form

我尝试制作一个带有类别和子类别的表单,它们都是复选框,并用它来设置我的服务类别和子类别。我尝试了一些方法,比如使用 SelectListItem,但我不能将它用于缩进复选框。 这是我的数据库图(我知道这不好,但客户想要它): Services Diagram

我为主要类别和子类别制作 SelectListItem 列表的方法:

public List<SelectListItem> GetAllMajorCategories()
    {
        return _context.CategoriesTbl
            .Where(c => c.ParentId == null)
            .Select(c => new SelectListItem
            {
                Value = c.CategoryId.ToString(),
                Text = c.Title
            }).ToList();
    }

    public List<SelectListItem> GetAllSubCategories()
    {
        return _context.CategoriesTbl
            .Where(c => c.ParentId == c.CategoryId)
            .Select(c => new SelectListItem
            {
                Value = c.ParentId.ToString(),
                Text = c.Title
            }).ToList();
    }

然后我在我的 PageModel 中使用这两种方法:

public void OnGet()
    {
        MainCategories = _serviceServices.GetAllMajorCategories();
        SubCategories = _serviceServices.GetAllSubCategories();
    }

然后在 Razorview 中,我想为每个 MajorCategory 和它的 SubCategories 生成复选框,但我不能。

    <div class="form-group">
@foreach (var item in Model.MainCategories)
    {
        <div class="icheckbox_minimal-blue">
        <input type="checkbox" class="minimal" id="majorCat" name="majorCat" value="@item.Value">
        <label> @item.Text </label>
        </div>
    }
</div>

我该怎么做?

According to your comment , the GetAllSubCategories() method did return the wrong list,you just need to retutn CategoriesTbl's list when ParentId is not null.

详情请参考以下代码:

PageModel.cs

 public void OnGet()
    {
        MainCategories = GetAllMajorCategories();
        SubCategories = GetAllSubCategories();
    }
    public List<SelectListItem> GetAllMajorCategories()
    {
        return _context.CategoriesTbl
     .Where(c => c.ParentId == null)
     .Select(c => new SelectListItem
     {
         Value = c.CategoryId.ToString(),
         Text = c.Title
     }).ToList();
    }

    public List<CategoriesTbl> GetAllSubCategories()
    {
        return _context.CategoriesTbl.Where(c => c.ParentId != null).ToList();
    }

查看:

@page
@model WebApplication1_rzaor_page.ShowCheckboxModel
@{
    ViewData["Title"] = "ShowCheckbox";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>ShowCheckbox</h1>

<div class="form-group"> 
    @foreach (var item in Model.MainCategories)
    {
        <div class="icheckbox_minimal-blue">
            <input type="checkbox" class="minimal" id="majorCat" name="majorCat" value="@item.Value">
            <label>@item.Text</label>
        </div>
        <ul>
            @foreach (var subitem in Model.SubCategories.Where(x => x.ParentId == Convert.ToInt32(item.Value)).ToArray())
            {
                <li>
                    <input type="checkbox" class="minimal" id="subCat" name="subCat" value="@subitem.CategoryId">
                    <label>@subitem.Title</label>
                </li>
            }

        </ul>
    }
</div>

这是结果: