Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NopViewComponent.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Nop.Core.Events;
using Nop.Core.Infrastructure;
using Nop.Web.Framework.Events;
using Nop.Web.Framework.Models;

namespace Nop.Web.Framework.Components;

/// 
/// Base class for ViewComponent in nopCommerce
/// 
public abstract partial class NopViewComponent : ViewComponent
{
    protected virtual void PublishModelPrepared(TModel model)
    {
        //Components are not part of the controller life cycle.
        //Hence, we could no longer use Action Filters to intercept the Models being returned
        //as we do in the /Nop.Web.Framework/Mvc/Filters/PublishModelEventsAttribute.cs for controllers

        //model prepared event
        if (model is BaseNopModel)
        {
            var eventPublisher = EngineContext.Current.Resolve();

            //we publish the ModelPrepared event for all models as the BaseNopModel, 
            //so you need to implement IConsumer> interface to handle this event
            eventPublisher.ModelPreparedAsync(model as BaseNopModel).Wait();
        }

        if (model is IEnumerable modelCollection)
        {
            var eventPublisher = EngineContext.Current.Resolve();

            //we publish the ModelPrepared event for collection as the IEnumerable, 
            //so you need to implement IConsumer>> interface to handle this event
            eventPublisher.ModelPreparedAsync(modelCollection).Wait();
        }
    }

    /// 
    /// Returns a result which will render the partial view with name .
    /// 
    /// The name of the partial view to render.
    /// The model object for the view.
    /// A .
    public new ViewViewComponentResult View(string viewName, TModel model)
    {
        PublishModelPrepared(model);

        //invoke the base method
        return base.View(viewName, model);
    }

    /// 
    /// Returns a result which will render the partial view
    /// 
    /// The model object for the view.
    /// A .
    public new ViewViewComponentResult View(TModel model)
    {
        PublishModelPrepared(model);

        //invoke the base method
        return base.View(model);
    }

    /// 
    ///  Returns a result which will render the partial view with name viewName
    /// 
    /// The name of the partial view to render.
    /// A .
    public new ViewViewComponentResult View(string viewName)
    {
        //invoke the base method
        return base.View(viewName);
    }
}