2016-03-21 7 views
1

私のページのオートコンプリート機能を作成しようとしています。 私はテキストボックスを持っており、私のデータベースから提案をしたいと思います。JSONでオートコンプリートASP.NET MVC

私は私のコントローラでこの化するJsonResultを持っている:

public JsonResult ItemAutocomplete(string term) 
{ 
    var result = _db.SelectTable("SELECT [i].[Name] from [dbo].[Item][i] WHERE [i].[Name] LIKE @0", SqlDb.Params(term +"%")); 
    return Json(result, JsonRequestBehavior.AllowGet); 
} 

と私の見解で:

@Scripts.Render("~/bundles/jqueryui") 
<h2>jQuery AutoComplete</h2> 
<script> 
     $(function() { 
      $('#tags').autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '@Url.Action("ItemAutocomplete")', 
         extraParams: { term: $('#tags').val(), 
         dataType: "json", 
         contentType: 'application/json, charset=utf-8', 
         data: { 
          term: $("#tags").val() 
         }, 
         success: function (data) { 

          response($.map(data, function (item) { 
           return { 
            label: item 
           }; 
          })); 
         }, 
         error: function (xhr, status, error) { 
          alert(error); 
         } 
        }); 
       }, 
       minLength: 2 
      }); 
     }); 

</script> 

<div class="ui-widget"> 
    <label for="tags">Tags: </label> 
    <input id="tags" /> 
</div> 

問題は、私が呼ぶ場合でも...私のItemAutocomplete化するJsonResultは常にヌルのparamを受信することですlocalhost/Appointment/ItemAutocomplete/item1のようにlocalhostから直接取得します。

+0

私は試しましたが、同じことをします。私はこのエラーも受け取ります: 'System.Reflection.RuntimeModule'型のオブジェクトをシリアル化している間に循環参照が検出されました。 –

+0

私はSQLクエリに疑問があります。コードビハインドにブレークポイントを入れましたか? _result_に何かが来ますか? – Bikee

+1

この問題を引き起こしているかどうかは不明ですが、 'extraParams:{term:$( '#tags')。val()、'は閉じています '}' –

答えて

3

使用

data: { term: request.term } 

の代わりに、オートコンプリートテキストボックスに

data: { term: $('#tags').val() } 

以下(request.term)、検索文字列 "request.term" によって検出。

+0

ありがとうございます! :) –

関連する問題