Asp.net-Mvc

Kendo ASP.NET MVC - 索引超出範圍數據綁定

  • August 18, 2013

嘗試將數據表綁定到 Telerik Kendo Grid 時出現以下錯誤:

指數超出範圍。必須是非負數且小於集合的大小。

我已經關注了 Telerik 的範例項目,但在我的程式碼中,“DataSource”的“讀取”功能似乎失敗了:

.Read(read => read.Action(“Read”, “Events”))

我有 ActionResult Read() 目前返回 null,只是因為我想看看它是否能實現該功能。此外,我已經逐步完成,數據表正在返回正確的列和列名。

任何幫助表示讚賞。

事件.cshtml

@model System.Data.DataTable

@{
   ViewBag.Title = "Events";
   Layout = "~/Views/_mainLayout.cshtml";
}

<p>Events</p>

@(Html.Kendo().Grid(Model)
   .Name("GridStatic")
   .Columns(columns =>
   {
       columns.Bound("ID");
       columns.Bound("EntryType");
       columns.Bound("EventDate");
       columns.Bound("EventData");
       columns.Bound("Source");
   })
   .Pageable()
   .Sortable()
   .Scrollable()
   .Filterable()
   .DataSource(dataSource => dataSource
       .Ajax()       
       .Model(model =>
           {
               model.Field("ID", typeof(int));
               model.Field("EntryType", typeof(DateTime));
               model.Field("EventDate", typeof(string));
               model.Field("EventData", typeof(string));
               model.Field("Source", typeof(string));
           })
       .Read(read => read.Action("Read", "Events"))
   )
)

事件控制器.cs

public class EventsController : Controller
{
   //
   // GET: /Events/

   public ActionResult Events(string sName)
   {
       EventReader ereader = new EventReader(sName);
       return View(ereader.ParseIntoTable(ereader.GetListOfEvents()));
   }

   public ActionResult Read([DataSourceRequest] DataSourceRequest request)
   {
       return null;
   }
}

更新::

這是堆棧跟踪,我不確定這是否有幫助。

[ArgumentOutOfRangeException:索引超出範圍。必須是非負數且小於集合的大小。
參數名稱:索引]
System.ThrowHelper.ThrowArgumentOutOfRangeException() +72
System.Collections.ObjectModel.Collection`1.set_Item(Int32 index, T value) +10451574
System.Web.Mvc.ControllerContext.get_RequestContext() +25
Kendo.Mvc.UI.NavigatableExtensions.GenerateUrl(INavigatable navigatable, ViewContext viewContext, IUrlGenerator urlGenerator) +52
Kendo.Mvc.UI.Fluent.CrudOperationBuilder.SetUrl() +81
Kendo.Mvc.UI.Fluent.CrudOperationBuilder.Action(String actionName, String controllerName, Object routeValues) +66
Kendo.Mvc.UI.Fluent.CrudOperationBuilder.Action(String actionName, String controllerName) +47
c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:40 中的 ASP._Page_Views_Events_Events_cshtml.b__3(CrudOperationBuilder 讀取)
Kendo.Mvc.UI.Fluent.AjaxDataSourceBuilderBase`2.Read(Action`1 配置器) +131
c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:30 中的 ASP._Page_Views_Events_Events_cshtml.b__2(DataSourceBuilder`1 數據源)
Kendo.Mvc.UI.Fluent.GridBuilder`1.DataSource(Action`1 配置器) +212
c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:16 中的 ASP._Page_Views_Events_Events_cshtml.Execute()
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext 上下文)+295
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.c__DisplayClass1a.b__17() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter 過濾器,ResultExecutingContext preContext,Func`1 延續)+242
System.Web.Mvc.c__DisplayClass1c.b__19() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 過濾器, ActionResult actionResult) +177
System.Web.Mvc.Async.c__DisplayClass2a.b__20() +89
System.Web.Mvc.Async.c__DisplayClass25.b__22(IAsyncResult asyncResult) +102
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
System.Web.Mvc.c__DisplayClass1d.b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.c__DisplayClass8.b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 結果) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

原來它與我的 web.config 有關。我創建的每個劍道對像都遇到了錯誤。將以下程式碼添加到頂級 web.config 似乎可以解決所有問題。

<runtime>
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
   <dependentAssembly>
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
     <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
   </dependentAssembly>
 </assemblyBinding>
</runtime>

通過在您的解決方案的 web.config 文件中包含部分將對您有所幫助。

<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
           <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
           <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
           <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
           <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
       </dependentAssembly>
   </assemblyBinding>
</runtime>

引用自:https://stackoverflow.com/questions/15386380