Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error:-The name 'productService' does not exist in the current context


My code Following:-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System.Web.Mvc;
using Kendo.Mvc.Examples.Models;


namespace Kendo.Mvc.Examples.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        public ActionResult Editing_Popup()
        {
            //ViewBag.Message = "Your contact page.";

            return View();
        }

         public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(productService.Read().ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Create(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                productService.Update(product);
            }

            return Json(new[] {product}.ToDataSourceResult(request,ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
        {
            if (product != null)
            {
                productService.Destroy(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

    }
    }
Posted
Updated 19-Nov-14 20:10pm
v2
Comments
Sinisa Hajnal 20-Nov-14 2:32am    
Well, the error says it all, it doesn't exist in HomeController. Where do you create it? Why do you think it should be visible in HomeController?
Nishant.Chauhan80 20-Nov-14 2:47am    
plz sir explain brief

1 solution

Hello Nishant,
you need to add the interface reference to the controller. I hope you are aware of dependency Injection and used a tool out of Unity, Ninject, StructureMap3 to resolve the injection.
For adding the reference you need to add a parameterized constructor to the controller like below:-
C#
public class HomeController : Controller
{
    private readonly IProductService _productService;
    public HomeController(IProductService productService)
     {
        _productService = productService;
     }
///then you can access each method defined in the Interface and implemented in the respective Service.
}

NOTE:- This will only work out, if you have used Dependency Resolver as mentioned above the Nuget Package tools. Else you will get error saying, No parameterless constructor defined
In the Interface:-
C#
namespace YOUR NAMESPACE GOES HERE
{
    public interface IProductService
    {
        void CreateProduct(MODEL/CLASS);
    }  
}
=================================================
public class ProductService : IProductService
{
    //Implement the methods declared in the Interface..
}


Hope this helps
Post back your queries if any.
Thanks
:)
 
Share this answer
 
v2
Comments
Nishant.Chauhan80 20-Nov-14 2:39am    
how ? please brief describe
[no name] 20-Nov-14 2:48am    
Are you aware of dependency Injection?
[no name] 20-Nov-14 2:53am    
Here you need to introduce dependency injection. This would let you access to the interface method.
Have you defined an interface?
[no name] 20-Nov-14 3:05am    
would you respond back so that this issue could be resolved sooner. :)
Nishant.Chauhan80 20-Nov-14 4:01am    
ok i will try solve this...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900