views:

161

answers:

1

I suspected there was some hidden magic somewhere that stopped what looks like actual method calls all over the place in T4MVC. Then I had a view fail to compile, and the stackTrace went into my actual method.

        [Authorize]
    public string Apply(string shortName)
    {
        if (shortName.IsNullOrEmpty())
            return "Failed alliance name was not transmitted";
        if (Request.IsAuthenticated == false || User == null || User.Identity == null)
            return "Apply authentication failed";
        Models.Persistence.AlliancePersistance.Apply(User.Identity.Name, shortName);
        return "Applied";
    }

So this method isn't generating in the template after all.

<%=Ajax.ActionLink("Apply", "Apply", new RouteValueDictionary() { { "shortName", item.Shortname } }, new AjaxOptions() { UpdateTargetId = "masterstatus" })%>

            <%=Html.ActionLink("Apply",MVC.Alliance.Apply(item.Shortname),new AjaxOptions() { UpdateTargetId = "masterstatus" }) %>

The second method threw an exception on compile because the method Apply in my controller has an [Authorize] attribute so that if someone that isn't logged on clicks this, they get redirected to login, then right back to this page. There they can click on apply again, this time being logged in.

And yes I realize one is Ajax.ActionLink while the other is Html.ActionLink I did try them both with the T4MVC version.

+2  A: 

Update: I see the problem. T4MVC only supports actions that return ActionResult, so it's not processing this particular action that returns string. You can fix this by changing it as follows:

    [Authorize]
    public ActionResult Apply(string shortName) {
        if (shortName.IsNullOrEmpty())
            return Content("Failed alliance name was not transmitted");
        if (Request.IsAuthenticated == false || User == null || User.Identity == null)
            return Content("Apply authentication failed");
        Models.Persistence.AlliancePersistance.Apply(User.Identity.Name, shortName);
        return Content("Applied");
    }

Note how it returns an ActionResult, and calls 'return Content("...")' instead of directly returning strings.


Could you give more details on the compile exception that you are getting? I assume this is something you see in the browser and not in VS? Could you include the full text of the error?

Generally, the T4MVC calls through the MVC prefix are never supposed to call the actual action method. Instead, they call an overridden method in a derived class. Look for a generated file called something like AllianceController.generated.cs (under T4MVC.tt). You should see an overridden 'Apply' method in there, which just does what T4MVC needs.

David Ebbo
I get the following warnings when I run custom tool on the template: Warning Compiling transformation: Assuming assembly reference 'EnvDTE, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' matches 'EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a', `you may need to supply runtime policy. Warning` Compiling transformation: `Unreachable code detected T4MVC.tt line 788`. and the apply method isn't being generated. Updating the question with the other code/exception info.
Maslow
That is just a benign warning which is unrelated to your issue.
David Ebbo