T4MVC using with external javascript files

947 Views Asked by At

Is it possible to use T4MVC in an external js file?

I include a js file in my cshtml file

and in MyScript.js i have the following in a function

@Url.Action(MVC.MyController.MyAction())

but it never get compiled to its Action name (thought that runat server would do that trick but it didn't)

what am i missing? I am also using chirpy if there is something that it can do to help...

thanks

2

There are 2 best solutions below

1
David Ebbo On

This is more of a general MVC question than T4MVC. T4MVC simplifies how you call @Url.Action, but whether you use T4MVC or not, you won't be able to have server code in a plain .js file.

However, there are some solutions around, like Using Inline C# inside Javascript File in MVC Framework

0
Shaddix On

T4MvcJs will help you handle that case. It generates a .js file which you can use in your external js-scripts.

Using it is a lot like T4Mvc: MvcActions.User.Index() will give you "/user/index".