【发布时间】:2018-07-08 21:37:48
【问题描述】:
我为 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;
}
我还检查了控制器,传递给控制器的关键字没有问题,查询运行正常并找到 1 行。但是 jquery 不会再次填充文本框...
【问题讨论】:
-
您尝试使用
id="appendprepend2"(无论是什么)将对象集合添加到您的元素中,这是没有意义的 -
result是一个数组,而不是单个字符串值。为什么要用数组填充输入元素? -
我通过 result[index number] 尝试它,但它不起作用我你解释我如何提取数组?
-
@StephenMuecke 我尝试的不仅仅是我在互联网上找到的样本......与“result.Name”、“result[1]”相同,但我只收到消息 [object object] 或 [undefined ]
-
我可以使用像这样的索引值 $('#appendprepend2').val(result[0].Name);但是当我使用for循环进行索引时它不起作用! for (i = 0; i
标签: jquery ajax asp.net-mvc asp.net-ajax