locked
How to bind a Repository in asp.net mvc controller RRS feed

  • Question

  • User-2035378889 posted

    Hello every I got this error when I call my controller 

    Error activating ILedgerRepository
    No matching bindings are available, and the type is not self-bindable.
    Activation path:
     2) Injection of dependency ILedgerRepository into parameter ledgerRepository of constructor of type LedgersController
     1) Request for LedgersController

    Suggestions:
     1) Ensure that you have defined a binding for ILedgerRepository.
     2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
     3) Ensure you have not accidentally created more than one kernel.
     4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
     5) If you are using automatic module loading, ensure the search path and filters are correct

    this is my controller

    using PharmaApp.Core.AppData;
    using PharmaApp.Data.AuthHelper;
    using PharmaApp.Core.DataModel;
    using PharmaApp.Core.Interface;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Web.Mvc;
    using PharmaApp.Core.ViewModel;
    using PharmaApp.Web.Reports;
    using System.Web.Security;


        public class LedgersController : Controller
        {     
          
            private readonly ILedgerRepository _ledgerRepository;

            public LedgersController(ILedgerRepository ledgerRepository)
               
          
            {
             
                _ledgerRepository = ledgerRepository;
            }
            // GET: Admin

            #region ledgers
            [HttpGet]
            public ActionResult Ledgers()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Ledgers(LedgersModel model)
            {
                if (ModelState.IsValid)
                {
                    _ledgerRepository.Insert(model);

                    foreach (var item in model.Items)
                    {
                        var stock = _ledgerRepository.All().SingleOrDefault(x => x.Acid == item.Acid);
                        // add new stock
                        _ledgerRepository.Insert(new LedgersModel { Acid = item.Acid, EntryId = item.EntryId, BillNo = item.BillNo, EntryDate = item.EntryDate, Deacripation = item.Deacripation, CrAmount = item.CrAmount, DrAmount = item.DrAmount, Sp = "GV" });

                    }
                    return Json(new { error = false, message = "Purchse saved successfully" });
                }
                return Json(new { error = true, message = "failed to save Purchse" });
            }

            public ActionResult List()
            {
                return View(_ledgerRepository.All());
            }

            [HttpGet]
            public ActionResult Editledgers(int ID)
            {
                var ledgers = _ledgerRepository.Find(ID);
                if (ledgers != null)
                {
                    return View(model: ID);
                }
                return RedirectToAction("List");
            }

            [HttpPost]
            public ActionResult Updateledgers(LedgersModel model)
            {
                if (ModelState.IsValid)
                {
                    // remove 
                    var items = _ledgerRepository.All().Where(x => x.Id == model.Id).ToList();
                    if (items.Any())
                    {
                        //subtract item quantity from stock
                        foreach (var item in items)
                        {
                            var stock = _ledgerRepository.All().SingleOrDefault(x => x.Acid == item.Acid);
                            // add new stock
                            _ledgerRepository.Insert(new LedgersModel { Acid = item.Acid, EntryId = item.EntryId, BillNo = item.BillNo, EntryDate = item.EntryDate, Deacripation = item.Deacripation, CrAmount = item.CrAmount, DrAmount = item.DrAmount, Sp = "GV" });

                        }
                        //remove from ledgers item
                        foreach (var item in items)
                        {
                            _ledgerRepository.Remove(item.Id);
                        }
                    }

                    var ledgers = _ledgerRepository.Find(model.Id);

                    foreach (var item in model.Items)
                    {
                        ledgers.Items.Add(new LedgersModel
                        {
                            Acid = item.Acid,
                            EntryId = item.EntryId,
                            BillNo = item.BillNo,
                            EntryDate = item.EntryDate,
                            Deacripation = item.Deacripation,
                            CrAmount = item.CrAmount,
                            DrAmount = item.DrAmount,
                            Sp = "Gv",

                        });
                    }
                    _ledgerRepository.Update(ledgers);

                    // add quantity to stock and or add new 


                    return Json(new { error = false, message = "Purchse updated successfully" });
                }
                return Json(new { error = true, message = "failed to Update Purchsed" });
            }

            public ActionResult Delete(int ID)
            {
                var items = _ledgerRepository.All().Where(x => x.Id == ID).ToList();
                if (items.Any())
                {
                    // subtract item quantity from stock

                }
                _ledgerRepository.Remove(ID);


                return RedirectToAction("List");
            }


            #endregion

        }I

    beaning a new comer in asp.net mvc I could not know how to troubleshoot to this error

    please help me 

    thanks to all

    Monday, March 22, 2021 6:03 PM

All replies

  • User475983607 posted

    I Google the error message and it to points to Ninject configuration.

    https://www.google.com/search?q=No+matching+bindings+are+available

    I recommend reading the Ninject support documentation to make sure you configured the services correctly.

    http://www.ninject.org/

    Monday, March 22, 2021 6:22 PM
  • User-2035378889 posted

    I resolved this issue to adding

    /// <summary>
            /// Load your modules or register your services here!
            /// </summary>
            /// <param name="kernel">The kernel.</param>
            private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind<ILedgerRepository>().To<LedgerRepository>();
                var dependencyServiceRegister = new DependencyServiceRegister();
                dependencyServiceRegister.Register(kernel);
            }

    in Ninject.Web.Common cs file

    Wednesday, March 24, 2021 9:22 PM