Asp.net mvc jQuery自动完成完全无响应

Asp.net mvc jQuery自动完成完全无响应,asp.net-mvc,asp.net-mvc-3,jquery,jquery-autocomplete,Asp.net Mvc,Asp.net Mvc 3,Jquery,Jquery Autocomplete,这是我第一次使用ASP MVC3 intranet应用真正深入研究jQuery。我需要自动完成,以便能够引用数据库中的项目列表。我跟着说,想“好吧,看起来很简单”。。。现在,在实现了代码,研究了其他方法,并用头敲击键盘至少四个小时之后,我再也没有像编写代码之前那样接近于完成这项工作 下面是视图中的代码,以及库声明。仅供参考-我将接管这个项目,因此您看到的所有其他javascript/Ajax都是由比我更有经验的其他人编写的。我把所有的代码都放在这一部分,以防有其他东西妨碍我 <link h

这是我第一次使用ASP MVC3 intranet应用真正深入研究jQuery。我需要自动完成,以便能够引用数据库中的项目列表。我跟着说,想“好吧,看起来很简单”。。。现在,在实现了代码,研究了其他方法,并用头敲击键盘至少四个小时之后,我再也没有像编写代码之前那样接近于完成这项工作

下面是视图中的代码,以及库声明。仅供参考-我将接管这个项目,因此您看到的所有其他javascript/Ajax都是由比我更有经验的其他人编写的。我把所有的代码都放在这一部分,以防有其他东西妨碍我

<link href="../../Content/jquery-ui-1.9.2.custom.css" rel="stylesheet">

<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">

    $(function () {
       $("#BankName").autocomplete({
          source: '@Url.Action("GetBanks", "AgentTransmission")',
          minLength: 1
    });

    $(function () {
        $("#drpProducerType").change(function () {
            var value = $(this).val();
            if (value == "SoleProprietor") {
                $("#Role").val(value);
                $('#DSSfields').removeClass('noSee');
                $('#DSSfields').addClass('seeMe');
                //alert("Role must be set to \"Sole Proprietor\" as well. Monet will do this for you!");
            }
            else {
                //TO DO: add role stuff here as well
                $('#DSSfields').removeClass('seeMe');
                $('#DSSfields').addClass('noSee');
            }

        });
        $("#Role").change(function () {
            var value = $(this).val();
            if (value == "SoleProprietor") {
                $("#drpProducerType").val(value);
                alert("Producer Type changed to \"Sole Proprietor\" as well");
            }

        });
    });


    function ChangeChannel() {
        //this function called if Market Segment changes, to update the channel
        var pendistcode = document.getElementById('Pendist');
        if (pendistcode == null) alert('Error: Cannot find Market Segment control');

        $.ajax({
            type: 'POST',
            url: '/AgentTransmission/GetChannel/',
            data: { pendist: pendistcode.value },
            success: function (data) {
                //                alert("success: " + data);
                $('#channelName').html(data);
                $('#Channel').val(data);
            },
            error: function (data) {
                alert("failure to obtain Channel name");
            }
        });

        CheckTerritory('channel');

    } //end ChangeChannel

    function CheckTerritory(category) {
        //this function called when changes happen that could change the territory (inddist)

        //if the channel changed, or the alignment indicator, update the Territory
        if ((category == "channel") | (category == "align")) UpdateTerritory();

        //only trigger if the state or zip changed on the aligned address 
        if ((category == "L") && ($('#AlignmentL').attr('checked'))) UpdateTerritory();
        if ((category == "M") && ($('#AlignmentM').attr('checked'))) UpdateTerritory();

    } //end CheckTerritory

    function UpdateTerritory() {

        var i = $('#INDDist').val();
        var p = $('#Pendist').val();
       // alert(":" + i + ":" + p + ":");

        //if ((i == "") || (p == "")) return;
        if (p == "") return;

        if ($('#INDDist').val() == "864") {
            $('#INDDist').val("701");
        }
        else {
            if ($('#INDDist').val() == "") {
                $('#INDDist').val("864");
            }
        }
    } //end UpdateTerritory

    function MoreCompanies(row) {
        //if the user clicks on the plus sign, add more company rows
        if (row == '3') {
            $('#plus2').html(' ');
            $('#row3').removeClass('noSee');
            $('#row3').addClass('seeMe');
        }
        if (row == '4') {
            $('#plus3').html(' ');
            $('#row4').removeClass('noSee');
            $('#row4').addClass('seeMe');
        }
        if (row == '5') {
            $('#plus4').html(' ');
            $('#row5').removeClass('noSee');
            $('#row5').addClass('seeMe');
        }

    } //end MoreCompanies

    function CompanyFields() {

    } //end CompanyFields

    function ShowHideTerritory() {
        alert('sunshine');
    } //end ShowHideTerritory

</script>
编辑

如果我用建议的代码替换当前的
.autocomplete
代码,我会在Chrome的调试器中得到以下错误:

Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method '/AgentTransmission/GetBanks'
这是新代码,我把它放在与我以前使用的完全相同的位置:

$(document).ready( function() {
  $('#BankName').autocomplete('@Url.Action("GetBanks", "AgentTransmission")', {
      dataType: 'json',
      parse: function(data) {
          var rows = new Array();
          for(var i=0; i<data.length; i++){
              rows[i] = { data:data[i], value:data[i].BankName };
          }
          return rows;
      },
      formatItem: function(row, i, n) {
          return row.BankName + ' - ' + row.Description;
      },
      width: 300,
      mustMatch: true,
  });
});
$(文档).ready(函数(){
$('#BankName').autocomplete('@Url.Action(“GetBanks”,“AgentTransmission”){
数据类型:“json”,
解析:函数(数据){
var rows=新数组();

对于(var i=0;i我在
autocomplete
中添加了一组额外的结束括号,这就清除了这个问题。小部件现在可以正常工作了

$(function () {
   $("#BankNameAuto").autocomplete({
      source: '@Url.Action("GetBanks", "AgentTransmission")',
      minLength: 1
    });
  }); 

该插件是否开始对数据库中只有一个字母的字符串进行查询(minLength:1)?您是否检查了查询本身的执行情况?演示显示minLength为2。我尝试了这两种方法,但两种方法都没有。您甚至收到服务器的响应吗?有任何类型的错误吗?检查了javascript控制台吗?模板函数是否解释为:'@Url.Action(“GetBanks”,“AgentTransmission”)“?我完全没有得到任何东西,没有错误,没有工作断点来显示代码正在javascript控制台或visual studio中执行任何类型的操作。它加载时没有Chrome的调试器中提到的任何错误。我确实知道
@Url.Action
有效,因为我们在其他页面上使用了该语法。@guessimtoolate:我能够使用其他方法设置错误消息。将其附加到我的帖子中。Thx!
$(document).ready( function() {
  $('#BankName').autocomplete('@Url.Action("GetBanks", "AgentTransmission")', {
      dataType: 'json',
      parse: function(data) {
          var rows = new Array();
          for(var i=0; i<data.length; i++){
              rows[i] = { data:data[i], value:data[i].BankName };
          }
          return rows;
      },
      formatItem: function(row, i, n) {
          return row.BankName + ' - ' + row.Description;
      },
      width: 300,
      mustMatch: true,
  });
});
$(function () {
   $("#BankNameAuto").autocomplete({
      source: '@Url.Action("GetBanks", "AgentTransmission")',
      minLength: 1
    });
  });