递归视图组件导致无限循环



>我正在尝试使用 ViewComponent 制作递归类别树,但我似乎陷入了无限循环,并在一两分钟后最终出现 HTTP 错误 502.3 - 网关错误。

我的想法是这样的:

  1. 在主视图中,使用空List<ViewModelProductCategory>作为参数调用组件,以指示我想首先获取根类别。

  2. ViewComponent 在数据库中查询具有ParentId == null的类别,并将其返回到Default.cshtml

  3. Default.cshtml中,该组件再次被调用,但这次使用子类别列表作为参数。

我像这样调用视图组件:

@await Component.InvokeAsync("SelectCategories", 
new List<MyStore.Models.ViewModels.ViewModelProductCategory> { })

ViewComponent 类如下所示:

using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyStore.Models;
using MyStore.Models.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyStore.Areas.Admin.ViewComponents
{
public class SelectCategoriesViewComponent : ViewComponent
{
private readonly MyStoreContext _context;
private readonly IMapper _mapper;
public SelectCategoriesViewComponent(MyStoreContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<IViewComponentResult> InvokeAsync(List<ViewModelProductCategory> catList)
{
List<ViewModelProductCategory> VM = await GetCategoriesAsync(catList);
return View(VM);
}
private async Task<List<ViewModelProductCategory>> GetCategoriesAsync(List<ViewModelProductCategory> catList)
{
List<ViewModelProductCategory> VM = new List<ViewModelProductCategory>();
if (catList.Count() == 0)
{
VM = _mapper.Map<List<ViewModelProductCategory>>
(await _context.ProductCategories
.Where(x => x.ParentId == null)
.ToListAsync());
}
else
{
VM = catList;
}
return VM;
}
}
}

视图组件的Default.cshtml如下所示:

@model IEnumerable<MyStore.Models.ViewModels.ViewModelProductCategory>
<ul style="list-style:none;padding-left:0px;">
@if (Model != null)
{
foreach (var item in Model)
{
<li style="margin-top:4px;padding:0px;">
@Html.HiddenFor(m => item.Id)
@Html.CheckBoxFor(m => item.Checked)
@Html.LabelFor(m => item.Id, item.Title)
<ul>
@await Component.InvokeAsync("SelectCategories", item.Children)
</ul>
</li>
}
}
</ul>

错误在哪里?

编辑

Tarek.Eladly的评论让我意识到我在GetCategoriesAsync方法中有一个糟糕的逻辑缺陷。我已经对我的代码进行了这些更改,现在它可以工作了:

首次调用视图组件:

@await Component.InvokeAsync("SelectCategories",
new
{
root = true,
catList = new List<MyStore.Models.ViewModels.ViewModelProductCategory>()
})

视图组件方法:

public async Task<IViewComponentResult> InvokeAsync(bool root, List<ViewModelProductCategory> catList)
{
List<ViewModelProductCategory> VM = await GetCategoriesAsync(root, catList);
return View(VM);
}
private async Task<List<ViewModelProductCategory>> GetCategoriesAsync(bool root, List<ViewModelProductCategory> catList)
{
List<ViewModelProductCategory> VM = new List<ViewModelProductCategory>();
if (root)
{
VM = _mapper.Map<List<ViewModelProductCategory>>
(await _context.ProductCategories
.Where(x => x.ParentId == null)
.ToListAsync());
}
else
{
VM = catList;
}
return VM;
}

Default.cshtml中调用视图组件:

@await Component.InvokeAsync("SelectCategories",
new
{
root = false,
catList = item.Children
})

:)

如果我是对的,只需查看(.Where(x => x.ParentId == null))GetCategoriesAsync你总是得到根类别,从不检查其他任何东西。

最新更新