2013-03-13 15 views
7

Dostaję następujący błąd, gdy próbuje powiązać DataTable do Telerik Kendo Grid:Kendo ASP.NET MVC - Indeks Out of Range Databinding

Index był poza zasięgiem. Musi być nieujemny i mniejszy niż rozmiar kolekcji.

Mam następnie przykładowy projekt Telerik, ale w moim kodu wydaje się być w przypadku braku na „przeczytane” funkcji „DataSource”:

.read (czytaj => read.Action ("Czytaj "," Wydarzenia "))

Mam ActionResult Read() obecnie zwraca wartość zerową, tylko dlatego, że chciałem sprawdzić, czy uda mu się uzyskać tę funkcję. Ponadto przeszedłem, a datatable zwraca poprawne kolumny i nazwy kolumn.

Każda pomoc jest doceniana.

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; 
    } 
} 

UPDATE ::

Oto ślad stosu, nie jestem pewien, czy to pomoże.

 
    [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

Jeśli zwrócisz pusty zestaw wyników, czy działa? –

+0

@ BrianMains - Ustawiłem, aby zwrócić pustą tabelę danych, ale otrzymałem ten sam błąd. – fortune

Odpowiedz

10

Okazało się, że był on związany z moim web.config. Podczas tworzenia każdego obiektu Kendo stworzyłem błędy. Dodanie następującego kodu do najwyższego poziomu web.config wydawało się naprawić wszystko.

<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

Jedną z możliwych przyczyn tego błędu jest możliwość załadowania starszej wersji systemu System.Web.Mvc w wersji starszej niż 4.0, jeśli projekt jest projektem starszym, do którego dodano interfejs użytkownika Kendo. Gdy dane są zwracane z serwera, format jest nierozpoznawalny i powoduje błąd. Dodając to do web.config najwyższego poziomu, zmuszasz najnowszą wersję do załadowania swojej witryny. – RCGoforth

+0

oszczędziłeś mi tyle czasu, dziękuję. – Menahem

0

chciałbym spekulować, że błąd występuje, ponieważ działanie Read nie zwraca poprawny zestaw wyników.

Zazwyczaj siatka Kendo wymaga zwracania wartości JsonResult podczas czytania w trybie Ajax. Jeśli zwrócisz pusty DataTable poprawnie sformatowany jako wynik json, to podejrzewam, że to naprawi.

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

Oczywiście należy zastąpić DataTable dla prawdziwego wyniku.

+0

Wprowadziłem tę zmianę, ale nadal otrzymuję błąd. Mam przerwę w wejściu do tej funkcji, ale ona nawet tego nie osiąga. Czy ten błąd może wynikać z tego, że nie można znaleźć akcji lub kontrolera? – fortune

+0

Czy możesz monitorować żądanie ajax w przeglądarce i zobaczyć, jaki kod odpowiedzi otrzymujesz? Kod statusu http wskazywałby, czy trasa nie została znaleziona, czy wystąpił błąd autoryzacji lub błąd serwera. – nukefusion

+0

Spojrzę na to teraz. Ponadto zaktualizowałem swój post ze śledzeniem stosu, nie jestem pewien, czy to będzie pomocne. – fortune

4

Włączenie sekcji pliku web.config rozwiązania pomoże.

<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> 
Powiązane problemy