我正在用asp.mvc 4和剃刀處理vs2010。 在我的項目到數據庫中我有一個表「人」,我需要自定義asp.net mvc的textboxfor,所以當我輸入名稱將顯示結果如附圖所示。 Facebook是搜索,我需要做的就是類似Facebook的人民搜索asp.net mvc自定義數據搜索的文本框
0
A
回答
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
相關問題
- 1. 帶自定義搜索框的Google自定義搜索?
- 2. jquery自定義搜索框
- 3. Asp.Net MVC 2 LabelFor自定義文本
- 4. 自定義數據註釋ASP.NET MVC C#
- 5. 用於搜索的ASP.NET MVC自定義路由
- 6. ASP.NET MVC 3 - 自定義搜索引擎友好的路線
- 7. jQuery的數據表自定義搜索框行爲
- 8. asp.net中的多重搜索文本框
- 9. 帶有自定義搜索框和按鈕的Google自定義搜索?
- 10. 谷歌自定義搜索框文本溢出一條線
- 11. 搜索私人數據庫的自定義搜索引擎
- 12. 在asp.net mvc網站上自定義搜索
- 13. 帶邊框的自定義搜索欄
- 14. Drupal的搜索框自定義
- 15. 數據庫訪問搜索文本框
- 16. 搜索數據庫 - ASP.NET MVC C#
- 17. 如何使用文本框在ASP.NET中搜索數據庫
- 18. jqGrid自定義窗體來搜索數據(選擇框問題)
- 19. asp.net的MVC搜索?
- 20. 谷歌自定義搜索的搜索框中的默認值
- 21. Layout.cshtml上的ASP.NET MVC搜索框
- 22. Elasticsearch:文本搜索中的字段級自定義分數
- 23. 如何創建自定義搜索框
- 24. 屬性網格自定義搜索框
- 25. Yii框架:自定義搜索
- 26. Android - 自定義快速搜索框
- 27. Sitecore - 自定義搜索對話框
- 28. Google的自定義搜索附加鏈接搜索框
- 29. Google自定義搜索的Google附加鏈接搜索框
- 30. 查詢獲取來自自定義搜索的特定數據
看的東西,如HTTP ://jqueryui.com/au tocomplete/ – PSL
嘗試使用Bootstrap Typeahead jQuery插件 - http://twitter.github.io/bootstrap/javascript.html#typeahead –
這需要在客戶端完成,並且將使用返回json的web方法。使用jquery自動完成。 –