2013-06-28 21 views
0

我正在用asp.mvc 4和剃刀處理vs2010。 在我的項目到數據庫中我有一個表「人」,我需要自定義asp.net mvc的textboxfor,所以當我輸入名稱將顯示結果如附圖所示。 Facebook是搜索,我需要做的就是類似Facebook的人民搜索asp.net mvc自定義數據搜索的文本框

textbox Search

+0

看的東西,如HTTP ://jqueryui.com/au tocomplete/ – PSL

+0

嘗試使用Bootstrap Typeahead jQuery插件 - http://twitter.github.io/bootstrap/javascript.html#typeahead –

+0

這需要在客戶端完成,並且將使用返回json的web方法。使用jquery自動完成。 –

回答

0

您可以使用自動完成(http://jqueryui.com/autocomplete/)。

一些代碼,可能是有用的,讓你開始:

VIEW:

// ...Other Stuff... 

    <input type="text" name="PersonName" id="PersonName" /> 


    @section Scripts { 

     @Styles.Render("~/Content/themes/base/css") 
     @Scripts.Render("~/bundles/jquery") 
     @Scripts.Render("~/bundles/jqueryui") 
     @Scripts.Render("~/bundles/jqueryval") 

     $("#PersonName").autocomplete({ 

       source: function (request, response) { 
        $.ajax({ 
         url: '@Url.Action("SearchPerson", "Person")', type: "GET", 
         dataType: "json", 
         data: { term: request.term }, 
         success: function (data) { 
          response($.map(data, function (item) { 
           return { Name: item.Name, Person: item.PersonID, value: item.Name }; 
          })) 
         } 
        }) 

       }, 
       minLength: 3, 
       select: function (event, ui) { 
        var selecteditem = ui.item; 

        // STUFF YOU WANT TO DO WITH THE SELECTED ITEM (like setting some HiddenField with an ID...) 

        // DEBUG: 
        //alert(ui.item ? ("You picked '" + selecteditem.Name : "Nothing selected"); 
       } 
      }); 

      return false; 
     }); 

    } 

控制器:

// GET : /Person/SearchPerson?term=term 

    public ActionResult AutocompletePerson(string term) 
    { 

     var people = from p in db.Person 
        where p.Name.StartsWith(term) 
        select new { PersonID = p.PersonID, Name = p.Name)}; 

     return this.Json(people, JsonRequestBehavior.AllowGet); 

    } 

乾杯

+0

嗨,我正在嘗試做同樣的事情,我跟着你的代碼,並工作正常。現在我想顯示圖像與name.How在autocomplete中追加圖像? –

+0

嘿。我從來沒有這樣做,所以我沒有任何關於此事的代碼。但這些鏈接可能是有用的:http://codeblogging.net/blogs/1/15/ http://stackoverflow.com/questions/911537/jquery-autocomplete-with-images http://stackoverflow.com/questions/12670956/jquery-ui-autocomplete -with-image祝你好運! – user1987392