Quantcast
Channel: DevNet Questions
Viewing all articles
Browse latest Browse all 8901

Override Invoke method in an inherited ViewComponent .net core 5.0

$
0
0
migrating from MVC ASP.net to MVC .Net Core.had a basepodswidgetcontroller that looked like this public abstract class BasePodsWidgetController@TProperties, TConfiguration, TItem@ : WidgetController@TProperties@ where TProperties : BaseWidgetProperties, new() where TConfiguration : TreeNode, IItemsListingWidgetConfiguration, new() where TItem : TreeNode, new() { protected readonly IComponentPropertiesRetriever _componentPropertiesRetriever; protected IContentRepository@TConfiguration@ _contentRepository; protected IContentRepository@PodContainer@ _parentRepository; protected IContentRepository@TItem@ _childrenRepository; protected abstract string ViewPath { get; } protected virtual int Limit { get; set; } = 5; public BasePodsWidgetController(IContentRepository@TConfiguration@ contentRepository, IContentRepository@PodContainer@ parentRepository, IContentRepository@TItem@ childrenRepository, IComponentPropertiesRetriever componentPropertiesRetriever) { _contentRepository = contentRepository; _parentRepository = parentRepository; _childrenRepository = childrenRepository; _componentPropertiesRetriever = componentPropertiesRetriever; } public virtual PartialViewResult Index() { var properties = _componentPropertiesRetriever.Retrieve@TProperties@(); ViewBag.MarginClass = WidgetStylingHelper.GetMarginClassFromWidgetProperties(properties.Margin); var widgetGuid = properties.WidgetConfiguration?.FirstOrDefault()?.NodeGuid; var widgetGuidValue = widgetGuid.HasValue ? widgetGuid.Value : Guid.Empty; var model = new PodsListing@TConfiguration, TItem@ { Configuration = _contentRepository.GetByNodeGuid(widgetGuidValue), Items = new TItem[0] }; if (null != model.Configuration) { var parent = _parentRepository.GetByNodeGuid(model.Configuration.ItemsParentNodeGUID); if (null != parent) { model.Items = _childrenRepository.GetChildrenByPath(parent.NodeAliasPath, Limit).ToArray(); } } return PartialView(ViewPath, model); } } which then allowed me to inherit and overide like so public class FeaturePodsWidgetController : BasePodsWidgetController@FeaturePodsWidgetProperties, FeaturePodsWidget, FeaturePod@ { protected override string ViewPath =@ @Widgets/_FeaturePods@; public FeaturePodsWidgetController(IContentRepository@FeaturePodsWidget@ contentRepository, IContentRepository@PodContainer@ parentRepository, IContentRepository@FeaturePod@ childrenRepository, IComponentPropertiesRetriever componentPropertiesRetriever) : base(contentRepository, parentRepository, childrenRepository, componentPropertiesRetriever) { } public override PartialViewResult Index() { var properties = _componentPropertiesRetriever.Retrieve@FeaturePodsWidgetProperties@(); var widgetGuid = properties.WidgetConfiguration?.FirstOrDefault()?.NodeGuid; var widgetGuidValue = widgetGuid.HasValue ? widgetGuid.Value : Guid.Empty; ViewBag.MarginClass = WidgetStylingHelper.GetMarginClassFromWidgetProperties(properties.Margin); var model = new PodsListing@FeaturePodsWidget, FeaturePod@ { Configuration = _contentRepository.GetByNodeGuid(widgetGuidValue), Items = new FeaturePod[0] }; if (null != model.Configuration) { var parent = _parentRepository.GetByNodeGuid(model.Configuration.ItemsParentNodeGUID); if (null != parent) { model.Items = _childrenRepository.GetChildrenByPath(parent.NodeAliasPath, 4).ToArray(); } } return PartialView(ViewPath, model); } } but converting to ViewComponents in .NetCore i get an error saying only one Invoke method is allowed and cant figure out whyBasePodsWidgetViewComponent[ViewComponent()] public abstract class BasePodsWidgetViewComponent@TProperties, TConfiguration, TItem@ : ViewComponent where TProperties : BaseWidgetProperties, new() where TConfiguration : TreeNode, IItemsListingWidgetConfiguration, new() where TItem : TreeNode, new() { protected readonly IComponentPropertiesRetriever _componentPropertiesRetriever; protected IContentRepository@TConfiguration@ _contentRepository; protected IContentRepository@PodContainer@ _parentRepository; protected IContentRepository@TItem@ _childrenRepository; protected abstract string ViewPath { get; } protected virtual int Limit { get; set; } = 5; public BasePodsWidgetViewComponent(IContentRepository@TConfiguration@ contentRepository, IContentRepository@PodContainer@ parentRepository, IContentRepository@TItem@ childrenRepository, IComponentPropertiesRetriever componentPropertiesRetriever) { _contentRepository = contentRepository; _parentRepository = parentRepository; _childrenRepository = childrenRepository; _componentPropertiesRetriever = componentPropertiesRetriever; } public virtual IViewComponentResult Invoke() { var properties = _componentPropertiesRetriever.Retrieve@TProperties@(); ViewBag.MarginClass = WidgetStylingHelper.GetMarginClassFromWidgetProperties(properties.Margin); var widgetGuid = properties.WidgetConfiguration?.FirstOrDefault()?.NodeGuid; var widgetGuidValue = widgetGuid.HasValue ? widgetGuid.Value : Guid.Empty; var model = new PodsListing@TConfiguration, TItem@ { Configuration = _contentRepository.GetByNodeGuid(widgetGuidValue), Items = new TItem[0] }; if (null != model.Configuration) { var parent = _parentRepository.GetByNodeGuid(model.Configuration.ItemsParentNodeGUID); if (null != parent) { model.Items = _childrenRepository.GetChildrenByPath(parent.NodeAliasPath, Limit).ToArray(); } } return View(ViewPath, model); } } Converted FeaturePodsWidgetViewComponent public class FeaturePodsWidgetViewComponent : BasePodsWidgetViewComponent@FeaturePodsWidgetProperties, FeaturePodsWidget, FeaturePod@ { public const string IDENTIFIER = @FeaturePodsWidget@; protected override string ViewPath =@ @_FeaturePodsWidget@; public FeaturePodsWidgetViewComponent(IContentRepository@FeaturePodsWidget@ contentRepository, IContentRepository@PodContainer@ parentRepository, IContentRepository@FeaturePod@ childrenRepository, IComponentPropertiesRetriever componentPropertiesRetriever) : base(contentRepository, parentRepository, childrenRepository, componentPropertiesRetriever) { } public override IViewComponentResult Invoke() { var properties = _componentPropertiesRetriever.Retrieve@FeaturePodsWidgetProperties@(); var widgetGuid = properties.WidgetConfiguration?.FirstOrDefault()?.NodeGuid; var widgetGuidValue = widgetGuid.HasValue ? widgetGuid.Value : Guid.Empty; ViewBag.MarginClass = WidgetStylingHelper.GetMarginClassFromWidgetProperties(properties.Margin); var model = new PodsListing@FeaturePodsWidget, FeaturePod@ { Configuration = _contentRepository.GetByNodeGuid(widgetGuidValue), Items = new FeaturePod[0] }; if (null != model.Configuration) { var parent = _parentRepository.GetByNodeGuid(model.Configuration.ItemsParentNodeGUID); if (null != parent) { model.Items = _childrenRepository.GetChildrenByPath(parent.NodeAliasPath, 4).ToArray(); } } return View(ViewPath, model); } } get an errorView component 'I3.Base.Web.ViewComponents.Widgets.FeaturePodsWidgetViewComponent' must have exactly one public method named 'Invoke' or 'InvokeAsync'and cant figure out why :(

Viewing all articles
Browse latest Browse all 8901

Trending Articles