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

穿精又带淫゛_ 提交于 2019-12-04 15:40:28

The <%= will not be evaluated in your external javascript.

Solution: pass the URL as a parameter to your javascript function.

<script>
    doAutoComplete('<%= Url.Action("Model", "AutoComplete") %>');
</script>

function doAutoComplete(url) {
    $("#model").autocomplete({ source: url });
}

You need to put the function call in a script block, and make sure jquery is loaded before your site.js ...

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

EDIT:

Maybe the '<%= ... =%>' tag isn't being evaluated server-side, before the function gets sent to the browser? Here is an article on this: http://www.west-wind.com/weblog/posts/252178.aspx

Here is a quote from the post:

There's also a problem if you need access to the [ASP.NET] variables in .js files - you can't embed script tags into a .js file so getting a dynamic value into a static file is problematic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!