ASP.NET MVC jQuery autocomplete with url.action helper in a script included in a page.

Posted by Boob on Stack Overflow See other posts from Stack Overflow or by Boob
Published on 2010-03-31T13:41:36Z Indexed on 2010/03/31 13:43 UTC
Read the original article Hit count: 308

I have been building my first ASP.NET MVC web app. I have been using the jQuery autocomplete widget in a number of places like this:

<head>
    $("#model").autocomplete({ source: '<%= Url.Action("Model", "AutoComplete") %>' });
</head>

The thing is I have this jQuery code in a number of different places through my web app. So i thought I would create a seperate javascript script (script.js) where I could put this code and then just include it in the master page. Then i can put all these repeated pieces of code in that script and just call them where I need too. So I did this. My code is shown below:

In the site.js script I put this function:

function doAutoComplete() {
    $("#model").autocomplete({ source: '<%= Url.Action("Model", "AutoComplete") %>' });
}


On the page I have:

<head>
    <script src="../../Scripts/site.js" type="text/javascript"></script>
    doAutoComplete();
</head>

But when I do this I get an Invalid Argument exception and the autocomplete doesnt work. What am I doing wrong? Any ideas?Do i need to pass something to the doAutoComplete function?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-ajax