Using classes from solution help

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi I am a new developer and I want apologize in advanced for this question

I am making a new plugin using the example plugin as a base

I have the following code

public class ONWWWController : Controller
    {
        public static ICategoryService _categoryService;

        public ActionResult Index()
        {
          
            return Json(_categoryService.GetAllCategories(), JsonRequestBehavior.AllowGet);
        }

What I want is a list of all product categories in the base solution, I know its like 2 lines of code to get it running, but I am a newbie in developing apps.

how do I call the code to return the data
Thanks in advance
11 years ago
You need a constructor to inject services.

    public class ONWWWController : Controller
    {
        private readonly ICategoryService _categoryService;

        public ONWWWController (
            ICategoryService categoryService)
        {

            this._categoryService = categoryService;
        }

Look at existing plugins and use one as a starting point that is closest to your needs.
Read about dependency injection
https://www.nopcommerce.com/docs/74/frequently-asked-development-questions.aspx
11 years ago
OK let me ask this is there something inbuilt in the app where I can list all the catagories and products without coding or will I have to code something ?

All I want is a read only list of these items ?

Thanks
11 years ago
using the following code

using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Core.Plugins;

namespace Nop.Plugin.Other.ONWWW.Controllers
{
    public class ONWWWController : Controller
    {
        public  ICategoryService _categoryService;

        public ONWWWController(ICategoryService categoryService)
        {
            this._categoryService = categoryService;
            
        }
        public ActionResult Index()
        {

            return Json(_categoryService.GetAllCategories(), JsonRequestBehavior.AllowGet);
        }
    }
    
}

I now get the error

No parameterless constructor defined for this object.
11 years ago
Did you register a route?
As per above, it's best to use an existing plugin as a starting point  (see RouteProvider.cs)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.