收到错误“名称'产品服务'在当前上下文中不存在

本文关键字:上下文 不存在 产品服务 名称 错误 | 更新日期: 2023-09-27 18:31:04

我在我的 Telerik 剑道网格中收到错误"名称'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));
    }
  }
}

收到错误“名称'产品服务'在当前上下文中不存在

我会开始研究依赖注入/IOC。这方面的一些例子是温莎城堡和宁注射。每个站点都有信息可帮助您入门。这个想法是,你的控制器上有依赖项的字段,这些字段又被传递到构造函数中并在那里设置,所以你需要的任何依赖项都将在创建控制器时存在。

对于一个简单的、自滚动的依赖注入解决方案,你可以有一个类来初始化你的所有单例和所有控制器,从那里传入所有相关的依赖关系。在您的应用程序启动中,注册所述类。

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

您没有名为"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