Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using T4MVC in a JS function

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>
like image 239
John Livermore Avatar asked Jan 19 '23 11:01

John Livermore


2 Answers

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>
like image 65
Stephen Avatar answered Jan 25 '23 07:01

Stephen


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)

like image 23
Shaddix Avatar answered Jan 25 '23 09:01

Shaddix