ASP.NET MVC jQuery的自动完成与脚本url.action帮手包含在一个页面 [英] ASP.NET MVC jQuery autocomplete with url.action helper in a script included in a page

查看:296
本文介绍了ASP.NET MVC jQuery的自动完成与脚本url.action帮手包含在一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在建设我的第一个ASP.NET MVC的Web应用程序。我一直在使用jQuery自动完成构件在许多地方是这样的:

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>

问题是我在许多不同的地方这个jQuery code到我的web应用程序。所以我想我会创建一个单独的javascript脚本(的script.js)在那里我可以把这个code,然后只需将其包含在母版页。然后,我就可以把code,所有这些作品多次在剧本,只是给他们打电话,我需要太多。所以,我这样做。我的code如下所示:


在site.js剧本我把这个功能:

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/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/site.js" type="text/javascript"></script>
    <script type="text/javascript">
        doAutoComplete();
    </script>
</head>

但是当我这样做,我得到一个无效参数异常,自动完成不工作。我究竟做错了什么?任何想法?我需要的东西传递给doAutoComplete功能?

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?

推荐答案

在&lt;%=不会在外部JavaScript进行评估

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

解决方案:通过URL作为参数传递给你的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 });
}

这篇关于ASP.NET MVC jQuery的自动完成与脚本url.action帮手包含在一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆