MVC应用程序中的错误修复 [英] Error Fix in MVC Application

查看:97
本文介绍了MVC应用程序中的错误修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误: - 当前上下文中不存在名称'productService'





我的代码如下: -



Error:-The name 'productService' does not exist in the current context


My code Following:-

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

    }
    }

推荐答案

Hello Nishant,

您需要将接口引用添加到控制器。我希望您了解依赖注入并使用Unity,Ninject,StructureMap3中的工具来解决注入。

要添加引用,您需要向控制器添加一个参数化构造函数,如下所示: -

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:-
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.
}



注意: - 如果你使用了Nuget Package工具上面提到的Dependency Resolver,这只会有用。否则你会收到错误说,没有定义无参数构造函数

在界面中: -


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:-

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


这篇关于MVC应用程序中的错误修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆