views:

158

answers:

2

I have a problem similar to the one described here: http://stackoverflow.com/questions/1128003/mvc-t4-mvctexttemplatehost-and-customized-controller-t4-template but hopefully, simpler.

I have a custom View templates (View.tt, Create.tt) for ASP.NET MVC project. I can't figure out how to get the controller name in these view templates. mvcHost.ControllerName and mvcHost.ControllerRootName both are null in View templates. However, somewhere in the pipeline controller name is known, since the view is created int the ControllerName folder. I want to refer to it in the template itself.

Specifically, I want to generate HTML.ActionLink("Edit", MVC.<controllername>.Edit(id)) instead of HTML.ActionLink("Edit", "Edit", new {id = item.id})

Is it possible to get the name of the controller in the View template?

A: 

Although this isn't strictly an answer to your question, why not use T4MVC?

Dan Atkinson
Sorry, Dan - not sure (a) why you think I am not using T4MVC - note that the code I am trying to generate is MVC.<controllername>.Edit(id)... in other words, I *am* trying to generate T4MVC code in the View; and (b) whether I am or am not - how would it help me to generate controller name in the view?
Felix
+1  A: 

I don't think there's a way to get that info in MVC 1.0 or MVC 2. The ControllerRootName and ControllerName properties from the host are for controller T4 templates and not for view T4 templates.

I'll see if we can add more info to the T4 host to enable these properties when generating views.

The reason that the generated file shows up in the right folder is that the MVC project system takes the output of the T4 template and puts it there. The T4 system itself doesn't participate in this process.

Eilon