是否可以在我的組件中放置多個 InvokeAsync?因為我的組件有時會回傳一些小東西,如果我為每個組件創建不同的檔案和類,就很難管理它們
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);
}
以及如何稱呼它?
@await Component.InvokeAsync("PriorityList", new { maxPriority = 4, isDone = true })
@await Component.myInvokeAsync2("PriorityList", new { maxPriority = 4, isDone = true })
uj5u.com熱心網友回復:
不,一個組件只能有一種InvokeAsync方法:
在 ASP.NET Core 中查看組件 | 微軟檔案
但是,如果您要呼叫的所有方法都采用相同的引數集,您可以傳入一個額外的引數來指定要呼叫的方法。
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 })
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408229.html
標籤:
上一篇:無法在表格中顯示資料
