处理递归类别生成代码时出现问题
因此,我构建了一个递归函数来生成 Category
对象的集合。
[ChildActionOnly]
public ActionResult FindAllCategorias()
{
var categoriasDb = _categoriaRepository.FindAllCategorias().Where(s => s.CategoriaPadreId == null);
List<CategoriaModel> model = new List<CategoriaModel>();
foreach (var categoria in categoriasDb)
{
model.Add(new CategoriaModel()
{
CategoriaId = categoria.CategoriaId,
Nombre = categoria.Nombre,
Encabezado = categoria.Encabezado
});
}
foreach (var categoriaModel in model)
{
categoriaModel.Subcategorias = FindSubcategoriesForCategory(categoriaModel.CategoriaId);
}
return PartialView(model);
}
private List<CategoriaModel> FindSubcategoriesForCategory(int id)
{
var subcategorias = _categoriaRepository.FindAllCategorias().Where(c => c.CategoriaPadreId == id);
List<CategoriaModel> subcategoriasModel = new List<CategoriaModel>();
foreach (var subcategoria in subcategorias)
{
subcategoriasModel.Add(new CategoriaModel()
{
CategoriaId = subcategoria.CategoriaId,
Nombre = subcategoria.Nombre,
Encabezado = subcategoria.Encabezado,
Subcategorias = FindSubcategoriesForCategory(subcategoria.CategoriaId)
});
}
return subcategoriasModel;
}
现在在我的视图中,您建议我如何使用递归来吐出我选择的模板中的每个类别?我不知道如何在视图中进行类似的操作。
So I have built a recursive function that generates a collection of Category
objects.
[ChildActionOnly]
public ActionResult FindAllCategorias()
{
var categoriasDb = _categoriaRepository.FindAllCategorias().Where(s => s.CategoriaPadreId == null);
List<CategoriaModel> model = new List<CategoriaModel>();
foreach (var categoria in categoriasDb)
{
model.Add(new CategoriaModel()
{
CategoriaId = categoria.CategoriaId,
Nombre = categoria.Nombre,
Encabezado = categoria.Encabezado
});
}
foreach (var categoriaModel in model)
{
categoriaModel.Subcategorias = FindSubcategoriesForCategory(categoriaModel.CategoriaId);
}
return PartialView(model);
}
private List<CategoriaModel> FindSubcategoriesForCategory(int id)
{
var subcategorias = _categoriaRepository.FindAllCategorias().Where(c => c.CategoriaPadreId == id);
List<CategoriaModel> subcategoriasModel = new List<CategoriaModel>();
foreach (var subcategoria in subcategorias)
{
subcategoriasModel.Add(new CategoriaModel()
{
CategoriaId = subcategoria.CategoriaId,
Nombre = subcategoria.Nombre,
Encabezado = subcategoria.Encabezado,
Subcategorias = FindSubcategoriesForCategory(subcategoria.CategoriaId)
});
}
return subcategoriasModel;
}
Now in my View, how do you suggestion I use recursion to spit out each Category in a template I choose? I'm not sure how to something like this in a View.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用递归显示模板:
然后为类别定义自定义显示模板 (
~/Views/Shared/DisplayTemplates/CategoriaModel.cshtml
):您还可以找到 以下帖子对于优化代码和数据访问很有用。
You could use a recursive display template:
and then define a custom display template for a category (
~/Views/Shared/DisplayTemplates/CategoriaModel.cshtml
):You may also find the following post useful in terms of optimizing your code and data access.
您可以尝试在方法中使用 MvcHtmlString.Create() 直接构建输出,也可以使用 razor 创建一个帮助程序来访问 ui 中的方法。
You could try building your output directly using MvcHtmlString.Create() within your methods or you can create a helper to access your methods in the ui using razor.