I am just trying to display nth relation using ul
and li
in razor using recursive function call. Suppose I have db table where I store parent child
Your code is not able to find a function ShowTree taking a parameter of type ICollection
when it executes the line
@ShowTree(item.Children)
because item.Children is of type ICollection
. The function ShowTree in your code takes a parameter of a different type, List
, which is not the same as ICollection
. As a result, the runtime reports the CS1502 error you see.
After realizing that you were looking for a recursive solution, I have modified the code to do just that.
Action Code
public ActionResult Index()
{
List
Razor Code
@{
var menuList = ViewBag.menusList as List;
@ShowTree(menuList);
}
@helper ShowTree(List menusList)
{
if (menusList != null)
{
foreach (var item in menusList)
{
-
@item.Name
@if (item.Children.Any())
{
@ShowTree(item.Children)
}
}
}
}