Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET MVC中没有为此对象定义无参数构造函数_C#_Asp.net Mvc_Design Patterns_Dependency Injection - Fatal编程技术网

C# 在ASP.NET MVC中没有为此对象定义无参数构造函数

C# 在ASP.NET MVC中没有为此对象定义无参数构造函数,c#,asp.net-mvc,design-patterns,dependency-injection,C#,Asp.net Mvc,Design Patterns,Dependency Injection,发布数据时,我没有为此对象错误定义无参数构造函数。它在get上运行良好。我在项目中使用服务模式,并使用unity容器处理依赖注入。我已在集装箱中注册了所有服务 堆栈跟踪:- [MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Bo

发布数据时,我没有为此对象错误定义无参数构造函数。它在get上运行良好。我在项目中使用服务模式,并使用unity容器处理依赖注入。我已在集装箱中注册了所有服务

堆栈跟踪:-

[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +122
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +239
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +85
   System.Activator.CreateInstance(Type type) +12
   System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +197

[MissingMethodException: No parameterless constructor defined for this object. Object type 'System.Web.Mvc.SelectList'.]
   System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +233
   System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult) +285
   System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +284
   System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17
   System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +377
   System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
   System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
   System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1210
   System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +333
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +336
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__0(AsyncCallback asyncCallback, Object asyncState) +640
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +346
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_0(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +27
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +494
   System.Web.Mvc.<>c.<BeginExecute>b__151_1(AsyncCallback asyncCallback, Object callbackState, Controller controller) +16
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +403
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_0(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +54
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +427
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +105
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
职位:-

    [HttpPost]
    public ActionResult Submit(HomeviewModel values)
    {


        return RedirectToAction("Index");
    }
视图模型:-

  public class HomeviewModel
 {
    public productDetailsDTO { get; set; }
    public CreationDTO ctDTO{ get; set; }
    public SelectList itemsList{ get; set; }
  }
控制器构造函数

 public HomeController(ICTService ctService,
        ITemsService itemService)
    {
        _ctService= ctService;
        _itemService= itemService;
    }
在我看来,我只是在DTO中使用itemslist和设置值,使用ajax获取一些数据。如果我们更改了所选的项目和提交表单,控件将进入控制器构造函数,但在给出错误后,不会进入post方法。

使用Castle。温莎:

 private readonly IIocManager _iocManager;
     ICTService _ctService= _iocManager.TryResolve<ICTService>() ??
                             throw new UserFriendlyException("UnknownActionType");
     ITemsService _itemService= _iocManager.TryResolve<ITemsService>() ??
                             throw new UserFriendlyException("UnknownActionType");
    public HomeController()
        {
        }
专用只读IIocManager\u iocManager;
ICTService _ctService=_iocManager.TryResolve()??
抛出新的UserFriendlyException(“UnknownActionType”);
ITemsService\u itemService=\u iocManager.TryResolve()??
抛出新的UserFriendlyException(“UnknownActionType”);
公共家庭控制器()
{
}

在asp.net mvc控制器中,必须添加不带参数的构造函数。 您应该添加此构造函数:

public HomeController()
{


    _ctService= new ICTService() ;
    _itemService= new ITemsService() ;
}  

您能提到您的依赖项注入设置吗?可能是
HomeviewModel
中存在的
SelectList
导致了此异常。也许您应该尝试将其更改为
IEnumerable
,不幸的是,这会破坏依赖注入的目的。在构造函数中接收接口的全部目的是将控制器与其依赖项的具体实现解耦<构造函数中的code>new将其紧密耦合。
public HomeController()
{


    _ctService= new ICTService() ;
    _itemService= new ITemsService() ;
}