I am creating viewcomponents and below are my all code..but when I call this viewcomponent, I am getting error like below..so any one can help me to solve this as well as explain me what exactly issue is.

The error is :

InvalidOperationException: Unable to resolve service for type 'PerfumeStore.Data.Models.ShoppingCart' while attempting to activate 'PerfumeStore.ViewComponents.ShoppingCartSummaryViewComponent'.

First of all I have created one folder with name ViewComponents in that I have created new class with name ShoppingCartSummaryViewComponent.cs and below is code of that.

using Microsoft.AspNetCore.Mvc;
using PerfumeStore.Data.Models;
using PerfumeStore.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PerfumeStore.ViewComponents
{
    public class ShoppingCartSummaryViewComponent : ViewComponent
    {
        private readonly ShoppingCart _shoppingCart;
        public ShoppingCartSummaryViewComponent(ShoppingCart shoppingCart)
        {
            _shoppingCart = shoppingCart;
        }

        public IViewComponentResult Invoke()
        {
            var items = _shoppingCart.GetShoppingCartItems();
            _shoppingCart.ShoppingCartItems = items;

            var shoppingCartViewModel = new ShoppingCartViewModel
            {
                ShoppingCart = _shoppingCart,
                ShoppingCartTotal = _shoppingCart.GetShoppingCartTotal()
            };

            return View(shoppingCartViewModel);
        }
    }
}

Now cshtml file is below in this path : in Views / Shared / Components / ShoppingCartSummary /Default.cshtml

@model ShoppingCartViewModel

@if (Model.ShoppingCart.ShoppingCartItems.Count > 0)
{
    <li>
        <a asp-controller="ShoppingCart">
            <span class="glphyicon glphyicon-shopping-cart"></span>
            <span id="cart-status">
                @Model.ShoppingCart.ShoppingCartItems.Count
            </span>
        </a>
    </li>
}

Now in my _Layout.cshtml file I am calling this viewcomponent in this way.

<div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
                @await Component.InvokeAsync("ShoppingCartSummary")
            </ul>
        </div>

But I am getting this error :

InvalidOperationException: Unable to resolve service for type 'PerfumeStore.Data.Models.ShoppingCart' while attempting to activate 'PerfumeStore.ViewComponents.ShoppingCartSummaryViewComponent'.

Thanks

2

There are 2 best solutions below

3
On

Dependending on your dependency lifecycle (transient, scoped, singleton) you can register it in your Startup.cs as:

Transient:

services.AddTransient<IMyDependency, MyDependency>();

Scoped:

services.AddScoped<IMyDependency, MyDependency>();

Singleton:

services.AddSingleton<IMyDependency, MyDependency>();

You can read more in the official docs

Note: If your ShoppingCart class does not have an interface you can ommit the base type when registering- services.AddTransient<MyDependency>();

0
On

Register class at ConfigureService method in Startup.cs file

services.AddScoped<ShoppingCart>();