作爲標籤助手的ViewComponent在ASP.NET Core 1.1中引入(請參閱Invoking a view component as a Tag Helper
here部分)。但以下內容僅返回Test for VC
部分視圖。看來<vc:annual-orders>...</vc:annual-orders>
部分根本不會被調用。作爲標籤助手的ViewComponent不會被調用
視圖\共享\組件\ AnnualOrders \ Default.cshtml:
@{
Layout = "";
}
<div>Test for VC</div>
<div>
<vc:annual-orders>
</vc:annual-orders>
</div>
的Myproj \ ViewComponents \ AnnualOrdersViewComponent.cs:
public class AnnualOrdersViewComponent : ViewComponent
{
private readonly OrdersContext _context;
public AnnualOrdersViewComponent(OrdersContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var lstOrders = _context.Where(t => t.orderType == "new");
return View(await lstOrders);
}
}
注:
- 我正在使用ASP.NET Core 1.1和沒有標籤助手的ViewComponents工作正常。
- 我也遵循此MSDN的official tutorial約
How to Invoke a View component as a tag helper
它解釋標籤助手的Pascal類和方法參數被翻譯成其較低的kebab case。