2012-10-05 45 views
0

我在我的MVC3 web項目中使用了ui jquery auto-complete,並將jquery保存在外部js文件中,並將其包含在網頁中,但它不起作用。如何用戶@ Html.Raw不工作在UI jQuery自動完成?

我用下面的代碼在js文件的用戶界面的jQuery自動完成:

$(function() { 
    $("#ReportsTo_FullName").autocomplete({ 
     source: function (request, response) { 
      var linkPath = "@Html.Raw(Url.Action("AutocompleteSuggestions", "Employee", new { @term = "Term", @moduleName="ModuleName"}))"; 
      linkPath = linkPath.replace("Term", request.term); 
      linkPath = linkPath.replace("ModuleName", "Employee"); 

      $.post(linkPath, function (data) { 
       response($.map(data, function (item) { 
        return { label: item.FullName, value: item.Id } 
       } 
       )); 
      }) 
      $.error(function (xhr, ajaxOptions, thrownError) { alert(thrownError); }) 
     }, 
     minLength: 1, 
     focus: function (event, ui) { 
      $("#ReportsTo_FullName").val(ui.item.label); 
      return false; 
     }, 
     select: function (event, ui) { 
      if (ui.item) { 
       $("#ReportsTo_FullName").css('border', ''); 
       $("#ReportsTo_FullName").val(ui.item.label); 
       $("#ReportsToId").val(ui.item.value); 
       return false; 
      } 
     }, 
     change: function (event, ui) { 
      if (ui.item == null) { 
       $("#ReportsTo_FullName").css({ 'border': '1px solid #ff0000' }); 
       $("#ReportsToId").val(null); 
      } 
     } 
    }); 
}); 

但它未能執行和它沒有顯示自動完成。

上面的jquery有什麼錯?

如何解決此問題?

+1

要解決這個問題,你應該先做一些研究。您可以查看[this](http://stackoverflow.com/q/7902213/944681),[this](http://stackoverflow.com/q/4599169/944681)或[this](http:// www .google.com)的首發.. –

回答

2

您不能使用此:

@Html.Raw(Url.Action("AutocompleteSuggestions", "Employee", new { @term = "Term", @moduleName="ModuleName"})) 

在js文件,這是服務器代碼。在頁面上創建隱藏字段,只有這樣你才能獲得這些數據。