asp.net-mvct4mvc

T4MVC: How to use with say Url.Action?


T4MVC documentation hints we can write

   @Url.Action(MVC.Dinners.Delete(Model.DinnerID))

indstead of the old fashioned and string hardcoded

   @Url.Action("Delete", "Dinners", ...))

(see attached pic)

enter image description here

Maybe I am seriously missing something, but the first version will actually call the controller's action method, what obviously not the goal + have side effects. I've followed the method call statically with resharper, and also debugged: The method was actually called, which (again) is not the goal.

It's true that Url.Action have overload which accepts ActionResult that's why the T4MVC version does not cause compile error. It's also true in case the action method can be called without side effect the url rendered correctly.

My goal is to render the action url in the view, with other words I am really seeking the alternative of @Url.Action("Delete", "Dinners", ...)).

Here is what I found:

 @Url.Action(MVC.Dinner.ActionNames.Delete, MVC.Dinner.Name)

What am I missing? (and why would we call the actual action method, just to render its url?)


Solution

  • That was fatal. All true what I described in the question, still it is false alert.

    My action method's return value was string. Please do not ask why, it is completely legal, and logically that method is void (called as POST from javascript, and response is ignored)

    That string return value fooled the T4MVC generator, so the method was actually called by the @Url.Action(MVC.Dinners.Delete(Model.DinnerID))

    After changing the method's return value to ActionResult (and doing nothing else) the T4MVC generated code is changed and the action method was not called anymore