我尝试在自动完成时显示数据库中的数据,并在用户使用 jquery,web service 选择时自动完成自动完成。但它没有显示,它显示所有数据,如对象也不会转到指定的URL。 请帮帮我。
function SearchText() {
debugger;
$(".searchtextbxclass").autocomplete({
source: function (request, response) {
debugger;
$.ajax({
url: "/Services/SearchService.asmx/SearchCategory",
data: "{ 'searchtxt': '" + request.term + "' }",
dataType: "json",
type: "POST",
/*contentType: "application/json; charset=utf-8",*/
contentType: "application/json;",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength:2,
select: function (event, ui) {
debugger;
$(this).val(ui.item.value);
doSearch(ui.item.value);
//$(this).closest('form').submit();
}
});
};
function doSearch(srchVal) {
debugger;
window.location.href = 'Default.aspx?searchtxt=' + searchtxt;
}
public class SearchService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<CategoryLevelModel> SearchCategory(string searchtxt)
{
List<CategoryLevelModel> categories = new List<CategoryLevelModel>();
CustSite_INDIAEntities ctx = new CustSite_INDIAEntities();
var cat1 = from b in ctx.tbl_CategoryLevel1 where b.Cat1_Name.Contains(searchtxt) select new { b.PID, b.Cat1_Name };
var cat2 = from b in ctx.tbl_CategoryLevel2 where b.Cat2_Name.Contains(searchtxt) select new { b.PID, b.Cat2_Name };
var cat3 = from b in ctx.tbl_CategoryLevel3 where b.Cat3_Name.Contains(searchtxt) select new { b.PID, b.Cat3_Name };
var cat4 = from b in ctx.tbl_CategoryLevel4 where b.Cat4_Name.Contains(searchtxt) select new { b.PID, b.Cat4_Name };
var cat5 = from b in ctx.tbl_CategoryLevel5 where b.Cat5_Name.Contains(searchtxt) select new { b.PID, b.Cat5_Name };
foreach (var item in cat1)
{
CategoryLevelModel cat = new CategoryLevelModel();
cat.level_id = 1;
cat.pid = item.PID;
cat.cat_name = item.Cat1_Name;
categories.Add(cat);
}
//Level 2
foreach (var item in cat2)
{
CategoryLevelModel cat = new CategoryLevelModel();
cat.level_id = 1;
cat.pid = item.PID;
cat.cat_name = item.Cat2_Name;
categories.Add(cat);
}
//Level 3
foreach (var item in cat3)
{
CategoryLevelModel cat = new CategoryLevelModel();
cat.level_id = 1;
cat.pid = item.PID;
cat.cat_name = item.Cat3_Name;
categories.Add(cat);
}
//Level 4
foreach (var item in cat4)
{
CategoryLevelModel cat = new CategoryLevelModel();
cat.level_id = 1;
cat.pid = item.PID;
cat.cat_name = item.Cat4_Name;
categories.Add(cat);
}
return categories;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SearchModule.Model
{
public class CategoryLevelModel
{
public double pid { get; set; }
public double level_id { get; set; }
public string cat_name { get; set; }
}
}