获取错误“当前上下文中不存在名称'productService'

时间:2014-11-20 11:15:41

标签: c# asp.net-mvc

我在Telerik Kendo网格中收到错误“名称'productService'在当前上下文中不存在”:

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));
    }
  }
}

2 个答案:

答案 0 :(得分:1)

我会开始研究依赖注入/ IOC 。其中一些例子是Castle WindsorNinject。每个网站都有相关信息可以帮助您入门。这个想法是你的控制器上有你的依赖项的字段,然后它们被传递到构造函数并设置在那里,所以你需要的任何依赖都将在创建控制器时存在。

对于一个简单的自动依赖注入解决方案,你可以有一个类来实现所有单例和所有控制器的实例化,从那里传递所有相关的依赖项。并在您的应用程序启动时,注册所述类。

Global.asax应用程序启动

 CompositionRoot = new CompositionRoot(); 
            HttpConfiguration config = GlobalConfiguration.Configuration;

            ControllerBuilder.Current.SetControllerFactory(CompositionRoot);
            config.Services.Replace(typeof(IHttpControllerActivator), CompositionRoot);
            var apiAuthenticationProvider = new ApiAuthenticationProvider(new HashGenerator());
            config.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider), new BasicAuthenticationFilterProvider(apiAuthenticationProvider));

撰写根

public sealed class CompositionRoot :IDisposable, IHttpControllerActivator, IControllerFactory
{
    // Singleton-scoped services are declared here...
    private readonly SingletonType_singletonInstance;
    public CompositionRoot()
    {
        // intitialise any application instance singletons
        _singletonInstance = new SingletonType()
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        // Per-Request-scoped services are declared and initialized here
        if (controllerType == typeof(TestController))
        {
            return new TestController(_singletonInstance)
        }
    }
}

答案 1 :(得分:0)

您还没有名为' productService'的成员字段或本地变量。你必须这样声明:

var productService = new ProductService();

或作为课堂上的一个字段:

private ProductService productService = new ProductService();

然后才能使用它。

这是关于字段的MSDN:http://msdn.microsoft.com/en-US/library/ms173118(v=vs.80).aspx

我还建议您从一开始就阅读本文,因为它教授了很多基础知识,可以阻止您在进步时挣扎:http://www.csharp-station.com/tutorial.aspx