Using T4MVC in JS Function

Take the script. Pay attention to the line '/ Home / Index'. Using T4MVC, is there a way to specify this to get rid of the magic string?

<script type="text/javascript"> $(document).ready(function () { $dialog = $('#dialog'); $dialog.dialog({ autoOpen: false, buttons: { }, open: function(event, ui) { $(this).load("/Home/Index"); } }); }); </script> 
+4
source share
3 answers

This is if your view uses Razor

 @Url.Action(MVC.Home.Index()) 

So your script will be

 <script type="text/javascript"> $(document).ready(function () { $dialog = $('#dialog'); $dialog.dialog({ autoOpen: false, buttons: {}, open: function (event, ui) { $(this).load("@Url.Action(MVC.Home.Index())"); } }); }); </script> 
+7
source

If your script is in a separate .js file (not in the Razor view), you can use T4MvcJS to handle this case.

This will look almost the same as in the Skuld example:

 $(this).load(MvcActions.Home.Index()); 

but it will be pure Javascript.

(T4MvcJs will generate a js helper - very similar to T4MVC)

+3
source

do something like

 $(this).load('<%:Url.Action("index","home") %>'); 
-1
source

All Articles