简体   繁体   中英

Loading a script with jQuery in .NET MVC3

I am currently loading a model with jQuery's .load. After it has been successfully loaded, I would like to execute some JavaScript which has dependencies on the loaded model.

buildFindOrderDialog: function () {
    workflowDialogContent.load('../../csweb/Orders/FindOrderDialog/', function () {
        $.getScript('~/Scripts/FindOrderDialogModel.js');
        workflowDialog.dialog('open');
    });
}

The load method executes the Order Controller's FindOrderDialog method which returns a ViewModel. After this has loaded, I want to run my FindOrderDialogModel javascript to affect the model client-side.

The above code does not properly load the javascript. I am wondering: does my controller have to provide a method for loading the javascript? The client has no concept of the relative path.

Of course, I could inline the script with the FindOrderDialog view, which would cause it to execute after load, but this seems like a bit of a hack.

UPDATE: My syntax was just a bit off. This works:

buildFindOrderDialog: function () {
    workflowDialogContent.load('../../csweb/Orders/FindOrderDialog/', function () {
        $.getScript('../Scripts/Orders/FindOrderDialogModel.js');
        workflowDialog.dialog('open');
    });
},

If I understand your question correctly, how about either

$.getScript('Scripts/FindOrderDialogModel.js');

Alternatively specify the full path:

$.getScript('/My/Full/Path/Scripts/FindOrderDialogModel.js');

Or finally, inject the relative path via ASP.NET:

$.getScript('<%= .NET CODE FOR RETRIEVING PATH USING THE ~ SIGN %>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM