【问题标题】:Cannot create an instance of an interface. IPagedList [closed]无法创建接口的实例。 IPagedList [关闭]
【发布时间】:2017-10-18 14:43:04
【问题描述】:

我正在尝试向我的应用程序中的结果显示表添加分页。我正在使用 PagedList 包来执行此操作。

首先,在我的模型中,我添加了:

public IPagedList<Object> PagedDisplayResults { get; set; } 

然后在控制器中,我加载列表的值:

 model.PagedDisplayResults = searchResults.ToPagedList(pageIndex, pageSize);

然后在我的视图中,我遍历 model.PagedDisplayResults 以显示我的结果。

在第一次加载/搜索/显示时,它可以无缝运行,但是当我进行后续搜索或触发回发的其他事件时,我收到以下错误并且没有控制器命中。我究竟做错了什么?似乎无法在 48 小时内找到解决方法,而且还在计数..

无法创建接口的实例。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.MissingMethodException:无法创建接口实例。

来源错误: 在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[MissingMethodException:无法创建接口的实例。] System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(布尔 publicOnly,布尔 skipCheckThis,布尔填充缓存,StackCrawlMark 和 stackMark)+113 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +206 System.Activator.CreateInstance(类型类型,布尔非公共)+83 System.Activator.CreateInstance(类型类型)+11 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +460 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1136 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +634 System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +59 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+653 System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +141 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,对象模型)+106 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2541 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +634 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +495 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +199 System.Web.Mvc.Async.c__DisplayClass25.b__1e(AsyncCallback asyncCallback, Object asyncState) +978 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate1 endDelegate, Object tag, Int32 timeout) +92 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate1 endDelegate, Object tag) +53 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +523 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +83 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调,对象状态,Int32 超时)+161 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate1 endDelegate, Object tag, Int32 timeout) +92 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +501 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调,对象状态,Int32 超时)+161 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate1 endDelegate, Object tag, Int32 timeout) +92 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +417 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +127 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调,对象状态,Int32 超时)+161 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记,Int32 超时)+92 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标记,Int32 超时)+97 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标签)+53 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) +373 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调, 对象状态) +88 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

    标签: c# asp.net-mvc pagedlist


    【解决方案1】:

    您应该有一个实现 IPagedList 的类,然后您可以创建该类的实例,但您不能直接在接口上创建实例

    public class MyClass:IPagedList<Object> {
    // implement interface methods and properties
    }
    
    public IPagedList<Object> MyMethod(){
      return new MyClass();
    }
    

    【讨论】:

    • OP 提到该属性是使用model.PagedDisplayResults = searchResults.ToPagedList(pageIndex, pageSize); 设置的,并且“在首次加载/搜索/显示时,它可以无缝运行”。
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多