11
Profil Bileşen: görünüşüdür ~/Görünümleruygun yapıcı
public class WidgetViewComponent : ViewComponent
{
private readonly IWidgetService _WidgetService;
private WidgetViewComponent(IWidgetService widgetService)
{
_WidgetService = widgetService;
}
public async Task<IViewComponentResult> InvokeAsync(int widgetId)
{
var widget = await _WidgetService.GetWidgetById(widgetId);
return View(widget);
}
}
/çalışanlar/Details.cshtml
@await Component.InvokeAsync("Widget", new { WidgetId = Model.WidgetId })
görünüşüdür bileşeni ~ Ziyaret bulunmaktadır /Shared/Components/Widget/Default.cshtml
aldığım hata aşağıdaki gibidir:
InvalidOperationException: 'MyApp.ViewComponents.WidgetViewComponent' türü için uygun bir kurucu bulunamadı. Türün beton olduğundan ve kamu kurucusunun tüm parametreleri için hizmetlerin kayıtlı olduğundan emin olun.
private WidgetViewComponent(IWidgetService widgetService) { _WidgetService = widgetService; }
aksi takdirde DI erişemez kamu olmalıdır: