0

i am getting multi-level dynamic menus from database. Currently i have called it successfully but i want to make it async.
here is my code;

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var menus = Uow.Menus.GetAll()
        .Select(m => new ParentMenuViewModel()
        {
            MenuId = m.MenuId,
            Name = m.Name,
            ParentId = m.ParentId
        })
        .ToList<ParentMenuViewModel>();
    ViewBag.Menus = CreateVM(0, menus);
    base.OnActionExecuting(filterContext);
}

public IEnumerable<ParentMenuViewModel> CreateVM(int parentid, List<ParentMenuViewModel> list)
{
    return list.Where(m=> m.ParentId==parentid)
        .Select(m=> new ParentMenuViewModel()
        {
            MenuId = m.MenuId,
            Name = m.Name,
            ParentId = m.ParentId,
            ChildMenus = CreateVM(m.MenuId, list)
        });
}

So, how can i make this asynchronous using async and await keywords?
Notice that, it loops until the children are loaded completely;
Is it even a good practice to make it async (the above code)?

Idrees Khan
  • 7,702
  • 18
  • 63
  • 111

1 Answers1

2

Unfortunately, async action filters are not available (yet); see this question. You can vote here for the MVC team to add this capability.

Once MVC supports async action filters, you could use, e.g., GetAllAsync. The recursive part does not require any async functionality because it's not I/O-bound.

Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810