4

Take the following script. Notice the string '/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>
John Livermore
  • 30,235
  • 44
  • 126
  • 216

3 Answers3

7

It's this if your view is using Razor

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

So your script would 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>
Stephen
  • 1,737
  • 2
  • 26
  • 37
3

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

It'll look almost the same to the Skuld's example:

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

but it'll be pure Javascript.

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

Community
  • 1
  • 1
Shaddix
  • 5,901
  • 8
  • 45
  • 86
-1

do something like

$(this).load('<%:Url.Action("index","home") %>');
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • That's not using T4MVC, that's just doing it the normal way. – Stephen Oct 18 '11 at 05:43
  • i believe, request will go through routing process before it finds its target and routing is responsible for generating url. – Muhammad Adeel Zahid Oct 18 '11 at 05:57
  • 2
    It will, but the problem is that Url.Action(string, string) is not T4MVC and is the normal MVC way of doing it. T4MVC is partly about replacing the 'magic strings' with coded values, so that when a controller/action is renamed, you get a compile time-error rather than a run-time error. The documentation explains it best http://mvccontrib.codeplex.com/wikipage?title=T4MVC_doc&referringTitle=T4MVC – Stephen Oct 18 '11 at 08:09