ASP MVC 和 Jquery ajax 搜索

ASP MVC and Jquery ajax Search

我编写了用于 ajax 搜索 return 用户信息的代码。

控制器:

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 没有再次填充文本框...

我找到了解决方案

        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;
    }