ArgumentOutOfRangeException in View

Dec 19, 2012 at 11:11 AM

Hello there,

I already posted this question on stackoverflow, but i would guess this is the best place to ask such question =)

 

I just installed T4MVC on my project and I run into some problem.

In my controller i can call redirect to action without problem:

  return RedirectToAction(Actions.Index());

If i do call it from my view, i get an ArgumentOutOfRangeException.

@Html.ActionLink("Delete Dinner", MVC.Home.Index())

To make sure I did it correctly, i created a new MVC solution and that line works.

Then I removed from my HomeController my 'baseController' inheritance and resintalled T4MVC to be sure it wouldn't interfere. Still no luck

I've no more idea where even start to look for this, debug doesn't help me as it seems to explose in the extention method:

[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) +10419142
   System.Web.Mvc.ControllerContext.get_RequestContext() +25
   System.Web.Mvc.Html.LinkExtensions.RouteLink(HtmlHelper htmlHelper, String linkText, String routeName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, IDictionary`2 htmlAttributes) +47
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result, IDictionary`2 htmlAttributes, String protocol, String hostName, String fragment) +196
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result) +72
   ASP._Page_Views_Home_Index_cshtml.Execute() in c:\Thva\Misc\DropBox\Work\MyProjects\Wims\Wims.Website\Views\Home\Index.cshtml:53

 

 I just tried this and it still doesn't work: Create a new controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Wims.Website.Controllers
{
    public partial class MyTestController : Controller
    {
        //
        // GET: /MyTest/

        public virtual ActionResult Index()
        {
            return View();
        }

    }
}

Run AutoT4MVC, View :

@{
    ViewBag.Title = "Index";
}
@Html.ActionLink("aiaieiae", MVC.MyTest.Index())

Index</h2>

To make sure i have no depedency, and it still doesn't work if i call my page:

http://localhost:2303/MyTest/index

Any idea? Thanks in advance
Coordinator
Dec 19, 2012 at 5:26 PM
Edited Dec 19, 2012 at 5:29 PM

Continuing discussion here as it's easier to go back and forth than on StackOverflow :)

So regular MVC ActionLink works for you. It'd be interesting if you could try exactly what T4MVC does when you call ActionLink. See the source code. So basically, when you call Html.ActionLink("aiaieiae", MVC.MyTest.Index()), it ends up calling the MVC LinkExtensions method:

 

RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

 

And for some reason that fails for you. To isolate, could you try manually making the same call to MVC without using T4MVC? BTW, what version of MVC and of T4MVC are you using?

Dec 19, 2012 at 7:27 PM
Edited Dec 19, 2012 at 7:37 PM

Hello, 

Sure we can keep the discussion here :)

Thanks for helping me, I really want to get T4MVC to work, it looks so good compared to normal actionlnk and the other :)


So i did what you suggested:

I copied the full code you sent me and changed the namespace to System.Web.Mvc2

I changed the name of the first Ex method there: 

 

 

    public static class T4Extensions2
    {
        public static MvcHtmlString ActionLinkUS(this HtmlHelper htmlHelper, string linkText, ActionResult result)
        {
            return htmlHelper.ActionLink(linkText, result, null, null, null, null);
        }

 

Changed my view in :

 

@Html.ActionLinkUS("aiaieiae", MVC.Home.About())

 

First time I ran it, it actually showed the link without crashing, but it did the same crash after i clicked on it. Not sure why (i suspect cache problem). After i 'debugged' it, it worked!


I guess that's a good step toward goodness? :)


I am using MVC4 on visual studio 2012, with the latest version of T4MVC from NuGet.


Another thing, on my desktop, i cannot install T4MVC, i get a serialization error. It does work on my laptop on the same project.Really strange.


Thanks again!

Coordinator
Dec 19, 2012 at 8:36 PM

I really can't explain that, but I still suspect there is a way to repro without involving T4MVC, as in the end all it does is make a standard MVC call. If you get a T4MVC-free repro, it's then easier to get some help from the standard MVC community.

Can you start a separate thread about the serialization error, so we don't mix the two issues here. Thanks!

Dec 20, 2012 at 5:38 AM

I think i understand what you are saying, but I never saw this issue before, not sure how i can reproduce it without T4MVC :/

Pretty sure it's not T4MVC's problem, I guess i could just restart a new project, injecting controller by controller see what happens?

Any idea other idea?

 

I will make another thread tonight (my time :)), about serialization.

 

Thanks!

Dec 20, 2012 at 6:21 AM

Hi David,

I just recreated a new project, reimported everything from my former project, and changed my main menu to work with T4MVC

                    
    "menu">
  • @Html.ActionLink("Home", MVC.Home.Index())
  • @Html.ActionLink("My Stuff", MVC.Stuff.Index())
  • @Html.ActionLink("Circle", MVC.Circle.Index())
  • @Html.ActionLink("Account", MVC.User.Edit())
  • @Html.ActionLink("Role", MVC.Role.Index())
  • @Html.ActionLink("About", MVC.Home.About())
  • @Html.ActionLink("Contact",MVC.Home.Contact())

This is working perfectly in my new project...

Really strange, but happy it does work! I can finally keep on going !

 

Once again, thanks for your time and you great project!

Coordinator
Dec 20, 2012 at 4:24 PM

I guess that's mixed news. Would be nice to know what was going on in case it happens again :)

Dec 20, 2012 at 6:26 PM

If you have any idea how i could try to reproduce i don t mind  doing it:) I kept my old project so i can reload it and test

Jul 17, 2013 at 1:16 PM
Edited Jul 17, 2013 at 2:44 PM
It just happened. All of a sudden I got the same error message and I have no idea on how to fix it.
Jul 17, 2013 at 2:46 PM
Alright so after an hour and a half I found the cause in my case. Inside web.config I added an xmlns attribute to the configuration element:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
originally it was just
<configuration>
By deleting the xmlns I got my web.config warnings back but my site is working again and I can continue my work.
Coordinator
Jul 17, 2013 at 6:05 PM
Yes, I think I've run into this once. But I'm almost certain it has nothing to do with T4MVC.