script in partial view

Topics: Troubleshooting
Feb 10, 2012 at 9:50 AM

In my partial view i want to add a script, but i keep getting the Error: '$' is undefined, when i render the view.

Here is an example:

@model TestModule.Models.Test

<script type="text/javascript">
    $
(document).ready(function () {
        alert
('partial is ready.');
   
});
</script>

<h1>This is my <%: Model.name %> partial view</h1>

Can someone give me a hint on whats the problem?, i have tried to put the script in a file and link it but it doens´t help.

 

Feb 10, 2012 at 10:03 AM

To make sure jQuery is included you need:

@{

Script.Require("jQuery");

}

Feb 10, 2012 at 11:23 AM

I have added Script.Require("jQuery"); but i still gets the same error, is this a problem when my view is a parital view? or do anyone else have any ideas?

 

Feb 10, 2012 at 11:32 AM

You could try Script.Require("jQuery").AtHead() to ensure that jQuery is loaded before your script executes.

Feb 10, 2012 at 11:47 AM

.AtHead made it!, thx for the help