This project is read-only.

Index was out of range. Must be non-negative and less than the size of the collection.

Aug 29, 2013 at 1:42 PM
Edited Aug 29, 2013 at 2:14 PM

I got the following exception when try to get Grid.Mvc working in my solution:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

I managed to integrate Grid.Mvc lately on another project, without any problem. The only difference between those 2 projects is that the current one is using .NET framework 4.5 as target framework. Does Grid.Mvc work under 4.5?

Here is the part of the exception stack trace:
[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ObjectModel.Collection`1.set_Item(Int32 index, T value) +14460247
   System.Web.Mvc.ControllerContext.get_RequestContext() +33
   GridMvc.Html.GridHtmlOptions`1.RenderPartialViewToString(String viewName, Object model, ViewContext viewContext) +221
   GridMvc.Html.GridHtmlOptions`1.ToHtmlString() +171
   System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, Object content) +17
Here is the Grid implementation on the view:
@Html.Grid(Model).Columns(columns =>
            columns.Add(o => o.Id).Sortable(true);
            columns.Add(o => o.Name).Sortable(true);
where Model is IQueryable<ViewModel>

I'm pretty sure that my implementation is correct, since i followed the Documentation and i already implemented this grid on previous project successfully.

Grid.Mvc version is 2.3.0 installed within NuGet package manager.

Do you have any idea what might be the problem?

Let me know if you need more details.

Thanks in advance.
Aug 29, 2013 at 2:19 PM
Hi, Looks intresting.

Based on the stack it seems trouble accessing to ViewContext.RequestContext, try to access in the the view:

@ViewContext.RequestContext It fails?
Aug 29, 2013 at 2:40 PM
Thanks for prompt response.

Yes, i managed to read the ViewContext on the view, i set the following code block as you said just before the Grid implementation:
    var context = ViewContext.RequestContext;
and the ViewContext was available at that point.

Are you interested in some particular value of ViewContext?
Aug 29, 2013 at 6:07 PM
Which version of MVC Framework are you using?
Aug 29, 2013 at 9:32 PM
Edited Aug 30, 2013 at 8:37 AM
MVC 4 (assembly version 4.0.20710.0)
Sep 2, 2013 at 4:10 AM
Very strange. I look at MVC 4 source code and I have no idea why it happens.
Can you modify grid settings? Make it more simple, for example:
@Html.Grid(Model).Columns(columns =>
            columns.Add(o => o.Id);
Is that affected the result?
Sep 2, 2013 at 10:56 AM
Bukharin, thank you for your help on this. I do appreaciate that! I managed to solve this problem on another way, so i'm going to share it:

I found the solution on the following discussion:

Actually, i missed the following section in my web.config file:
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
During adding of new project, i picked Empty MVC4 template and that section was not included within that option (it is included with Basic template).

I hope that will help others in the future and i'm glad that i managed to get this grid working, since i found this plugin very nice and i like to use it.

Best regards!
Oct 1, 2013 at 9:34 AM
Hi, I have the same problem but dpalezevic's solution is not helping!

I use MVC 4.
Oct 2, 2013 at 5:00 AM
Hi, Jsalichos

Which version of the grid you are using? Are you migrate your project from MVC 3/2 ? Try to create new MVC 4 app and reproduve the problem