0

作爲標籤助手的ViewComponent在ASP.NET Core 1.1中引入(請參閱Invoking a view component as a Tag Helperhere部分)。但以下內容僅返回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); 
    } 
} 

注:

  1. 我正在使用ASP.NET Core 1.1和沒有標籤助手的ViewComponents工作正常。
  2. 我也遵循此MSDN的official tutorialHow to Invoke a View component as a tag helper它解釋標籤助手的Pascal類和方法參數被翻譯成其較低的kebab case

回答

0

我一直在爲此努力,終於設法讓視圖組件工作的標籤助手。 我遇到的問題是標籤助手不在區域內的視圖上工作。 爲了解決這個問題,我將/ Views目錄中的_ViewImports.cshtml和_ViewStart.cshtml頁面複製到/ Areas // Views目錄中,並且標記幫助程序現在可以工作,並且VS正在爲我的屬性提供intellisense。

不要忘記添加到_ViewStart.cshtml文件(其中是包含View組件的程序集的名稱:

@addTagHelper *, <AssemblyName>