T4MVC does not generate action
I suspected that there was some hidden magic in there to stop what appears to be calling real methods all over the place in T4MVC. Then I was unable to compile the view and 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 is not generated in the template.
<%=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 a compile-time exception because the method Apply
in my controller has an attribute [Authorize]
, so if someone who is not logged in clicks on this, it is redirected to logged in and then back to this page. There they can click on the application again, this time logged in.
And yes, I understand that one of them is Ajax.ActionLink
and the other is Html.ActionLink
. I tried them both with the T4MVC version.
source to share
Update: I see a problem. T4MVC only supports actions that return an ActionResult, so it doesn't handle that specific action that returns a string. You can fix this by changing it like this:
[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");
}
Notice how it returns an ActionResult and calls "return Content (" ... ")" instead of returning rows directly.
Could you please give more details on the compilation exception you are getting? I assume this is what you see in the browser and not in VS? Could you include the full text of the error?
Generally, calls to T4MVC through the MVC prefix should never call the actual action method. Instead, they call an overridden method in a derived class. Take a look at the generated file called something like AllianceController.generated.cs (under T4MVC.tt). You should see an overridden Apply method that just does what T4MVC wants it to do.
source to share