2013-03-13 44 views
7

試圖綁定一個DataTable的Telerik的劍道網格時,我發現了以下錯誤:劍道ASP.NET MVC - 指數超出範圍的數據綁定

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

我已經按照Telerik的示例項目,但在我的代碼,它似乎是在「數據源」的「讀取」功能失效:

.Read(讀=> read.Action(「讀「),」Events「))

我有ActionResult Read()當前返回null,僅僅是因爲我想看看它是否甚至會使它成爲該函數。另外,我已經介紹過了,數據表返回正確的列和列名稱。

任何幫助表示讚賞。

Events.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")) 
    ) 
) 

EventsController.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: Index was out of range. Must be non-negative and less than the size of the collection. 
    Parameter name: index] 
    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 
    ASP._Page_Views_Events_Events_cshtml.b__3(CrudOperationBuilder read) in c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:40 
    Kendo.Mvc.UI.Fluent.AjaxDataSourceBuilderBase`2.Read(Action`1 configurator) +131 
    ASP._Page_Views_Events_Events_cshtml.b__2(DataSourceBuilder`1 dataSource) in c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:30 
    Kendo.Mvc.UI.Fluent.GridBuilder`1.DataSource(Action`1 configurator) +212 
    ASP._Page_Views_Events_Events_cshtml.Execute() in c:\Users\wsharp\Documents\Visual Studio 2010\Projects\Inviso\Inviso\Views\Events\Events.cshtml:16 
    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 context) +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 filter, ResultExecutingContext preContext, Func`1 continuation) +242 
    System.Web.Mvc.c__DisplayClass1c.b__19() +21 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, 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 result) +9 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 
+0

如果你返回一個空的結果集,它工作嗎? – 2013-03-13 13:32:51

+0

@BrianMains - 我設置它返回一個空的數據表,但得到了同樣的錯誤。 – fortune 2013-03-13 13:41:52

回答

10

原來它與我的web.config有關。我遇到了我創建的每個Kendo對象的錯誤。將以下代碼添加到頂層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> 
+1

錯誤的可能原因之一是,如果您的項目是已添加Kendo UI的「傳統」項目,則可以加載比4.0版本的System.Web.Mvc更早的項目。從服務器返回數據時,格式無法識別並導致錯誤。通過在頂級web.config中添加這個功能,您可以強制使用您的站點加載的最新版本。 – RCGoforth 2013-06-13 13:49:29

+0

你救了我這麼多時間,謝謝。 – Menahem 2013-10-29 10:24:28

0

我推測錯誤發生,因爲Read動作沒有返回一個有效的結果集。

通常Kendo網格要求在使用Ajax模式讀取時返回JsonResult。如果你返回一個空的DataTable格式正確的json結果,那麼我懷疑它會修復它。

public ActionResult Read([DataSourceRequest] DataSourceRequest request) 
    { 
    return this.Json(new DataTable().ToDataSourceResult(request)); 
    } 

顯然,你應該替代DataTable爲您的真實結果集。

+0

我做了這個改變,但我仍然收到錯誤。我在該函數的入口處有一個斷點,但它甚至沒有達到這個目的。這個錯誤是否能夠找到操作或控制器? – fortune 2013-03-13 15:05:25

+0

你可以在瀏覽器中監控ajax請求,看看你收到了什麼響應代碼? http狀態代碼會告訴您路由是否未找到,或者是否存在授權問題或服務器錯誤。 – nukefusion 2013-03-13 15:08:56

+0

我現在就來看看。另外,我已經用堆棧跟蹤更新了我的帖子,我不確定這是否會有所幫助。 – fortune 2013-03-13 15:11:59

4

通過包含在解決方案中的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>