ASP MVC and Jquery ajax Search



我为ajax搜索编写了返回用户信息的代码。

控制器:

public JsonResult SearchPeopleByName1(string keyword)
{
System.Threading.Thread.Sleep(2000);
ApplicationDbContext myDbContext = new ApplicationDbContext();
var data = myDbContext.UserProfiles.Where(f =>
f.ApplicationUser.UserProfile.Name.StartsWith(keyword)).ToList();
return Json(data,JsonRequestBehavior.AllowGet);
}

Jquery :

function Search() {
$.ajax({
url: "/doctor/Home/SearchPeopleByName1/" ,
data: "keyword=" + $('#appendprepend2').val(),
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (result) {
$('#appendprepend2').text(result + "yes");
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}

我还检查控制器,关键字传递给控制器没有问题,查询运行 okey 并找到 1 行。 但是jquery不会再次填充文本框...

我找到了解决方案

function Search() {
$.ajax({
url: "/doctor/Home/SearchPeopleByName1/",
data: "keyword=" + $('#appendprepend2').val(),
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function(result) {
var i = 0;
for (i; result.length >= i ; i++) {
$('#appendprepend2').val($('#appendprepend2').val() + result[i].Name);
}

},
error: function(errormessage) {
alert(errormessage.responseText);
}
});
return false;
}

最新更新