尝试创建类型为X的控制器时发生错误。确保控制器具有无参数构造函数

时间:2014-05-13 07:04:09

标签: c# asp.net asp.net-mvc

我正在开发一个asp.net mvc项目,在那里我创建了一个带有一些下拉菜单的局部视图。这些是用ajax填充的,应该用作过滤器。 (我猜这与我的问题不太相关,但无论如何都很高兴。)

尝试调用局部视图时出现问题,说明如下: An error occurred when trying to create a controller of type 'Project.Name.Web.Controllers.PlanFilterController'. Make sure that the controller has a parameterless public constructor.

但是,我的控制器似乎确实有一个无参数构造函数;

public class PlanFilterController : BaseController
    {
        public PlanFilterControllerViewModel Model { get; set; }
        public List<DropDownItem> Items { get; set; }

        public List<ProjectContract> AllProjects { get; set; }
        public List<CustomerContract> AllCustomers { get; set; }
        public List<UnitContract> AllUnits { get; set; }

        private ProjectServiceClient ProjectClient { get; set; }
        private CustomerServiceClient CustomerClient { get; set; }

        public PlanFilterController()
        {
            AllProjects = new List<ProjectContract>();
            AllCustomers = new List<CustomerContract>();
            AllUnits = new List<UnitContract>();

            ProjectClient = new ProjectServiceClient();
            CustomerClient = new CustomerServiceClient();

            AllProjects = ProjectClient.GetProjects().ToList();
            AllCustomers = CustomerClient.GetCustomers().ToList();
            AllUnits = UnitClient.GetUnits(true, "", false).ToList();
        }

        // GET: /PlanFilter/
        [ChildActionOnly]
        public ActionResult FilterControl()
        {

            return PartialView();
        }

        // Populate dropdowns
        public JsonResult GetCascadeCustomers()
        {
            Items = new List<DropDownItem>();

            foreach (var customer in AllCustomers)
            {
                Items.Add(new DropDownItem
                {
                    ItemId = customer.Id,
                    ItemName = customer.Name
                });
            }

            return Json(Items, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCascadeProjects()
        {
            Items = new List<DropDownItem>();

            foreach (var project in AllProjects)
            {
                Items.Add(new DropDownItem
                {
                    ItemId = project.Id,
                    ItemName = project.Name
                });
            }

            return Json(Items, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCascadeUnits()
        {
            Items = new List<DropDownItem>();

            foreach (var unit in AllUnits)
            {
                Items.Add(new DropDownItem
                {
                    ItemId = unit.Id,
                    ItemName = unit.Name
                });
            }

            return Json(Items, JsonRequestBehavior.AllowGet);
        }
    }

我在这里缺少什么?此外,如果有人可以请解释为什么会这样,那将是不错的:))

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,当我添加服务引用时发生了一些事情,并且没有自动生成某些代码。这又引起了上述例外。