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. I removed from my HomeController my 'baseController' inheritance and resintalled T4MVC to be sure it wouldn't interfere. 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
Any idea? Thanks in advance
Edit: 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())
<h2>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
I had the same issue. Then I found the following warnings in build log:
I did what the warning message suggested by adding the following lines to web.config:
And that was it - T4MVC works fine. Sometimes, NuGet adds these lines automatically, I don't know why it didn't so this time.
This solution also explains why creating a new web project and copying source codes might solve this issue.
I've looked into this with David Ebbo (thanks a lot to him for his time), and could not find why it's not working.
After copy pasting T4MVC code in my project with my own name space and use those methods it was working..
So, i just recreated a new MVC Website and reimported everything.. not fun but i believe T4MVC worths the trouble :)
Ok, so I know my answer is rather late but it may help others. In my case everything went well until all of the sudden I had this error:
In my case the problem was that inside web.config I have added an xmlns attribute to the configuration element:
Originally it was just
By deleting the xmlns I got my web.config warnings back but my site is working again and I can continue my work.
[Update]
I wanted to add what I think is the real solution to this problem but I saw that gius bellow already wrote it. So yeah, the issue is a missing <bindingRedirect> in web config:
Based on the stack, it looks like it has trouble accessing the request context, which is not related to T4MVC. To isolate, can you remove the T4MVC line and simply try to write:
in your view? You can also try
@Html.ViewContext.RequestContext
. I'm guessing you'll see the same exception. If so, I'm not sure what causes it, but at least it gets you a step closer.Another thing to try: does the regular MVC
ActionLink
work, or is it only T4MVCActionLink
that fails?