ASP MVC和Jquery ajax搜索

时间:2018-07-08 21:37:49

标签: jquery ajax asp.net-mvc asp.net-ajax

我为返回用户信息的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不会再次填充文本框...

1 个答案:

答案 0 :(得分:0)

我找到了解决办法

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