is it possible to put more than one InvokeAsync in my components? because my components are sometimes returned small things and if I create different files and classes for each one it's hard to manage them
public async Task<IViewComponentResult> InvokeAsync(
int maxPriority, bool isDone)
{
string MyView = "Default";
// If asking for all completed tasks, render with the "PVC" view.
if (maxPriority > 3 && isDone == true)
{
MyView = "PVC";
}
var items = await GetItemsAsync(maxPriority, isDone);
return View(MyView, items);
}
public async Task<IViewComponentResult> myInvokeAsync2(
int maxPriority, bool isDone)
{
string MyView = "Default";
// If asking for all completed tasks, render with the "PVC" view.
if (maxPriority > 3 && isDone == true)
{
MyView = "PVC";
}
var items = await GetItemsAsync(maxPriority, isDone);
return View(MyView, items);
}
and how to call it?
@await Component.InvokeAsync("PriorityList", new { maxPriority = 4, isDone = true })
@await Component.myInvokeAsync2("PriorityList", new { maxPriority = 4, isDone = true })
CodePudding user response:
No, a component can only have one InvokeAsync method:
View components in ASP.NET Core | Microsoft Docs
However, if all of the methods you want to invoke take the same set of parameters, you you could pass in an extra parameter to specify the method to call.
public Task<IViewComponentResult> InvokeAsync(
int method, int maxPriority, bool isDone) => method switch
{
2 => Method2(maxPriority, isDone),
_ => Method1(maxPriority, isDone),
}
private async Task<IViewComponentResult> Method1(
int maxPriority, bool isDone)
{
...
}
private async Task<IViewComponentResult> Method2(
int maxPriority, bool isDone)
{
...
}
@await Component.InvokeAsync("PriorityList", new { method = 1, maxPriority = 4, isDone = true })
@await Component.InvokeAsync("PriorityList", new { method = 2, maxPriority = 4, isDone = true })
