简体   繁体   English

jQuery Autocomplete,MVC4和WebAPI

[英]jQuery Autocomplete, MVC4, and WebAPI

I downloaded the source code for a tutorial on autocompleting text boxes (eg start typing and a drop-down will appear with options matching those first few characters) from http://www.codeproject.com/Tips/639578/jQuery-Autocomplete-MVC-and-WebAPI - I can compile it and it works perfectly. 我从http://www.codeproject.com/Tips/639578/jQuery-Autocomplete-下载了有关自动填充文本框的教程的源代码(例如,开始键入,并且下拉列表将显示与前几个字符匹配的选项) 。 MVC和WebAPI-我可以对其进行编译,并且可以完美运行。

Trying to adapt it for a username lookup and I'm having problems. 尝试使其适应用户名查找,但出现了问题。 Everything compiles okay but the drop-down is not firing. 一切都可以编译,但下拉菜单不触发。 I'm missing something but I've no idea what! 我想念什么,但我不知道该怎么办!

ApiController: ApiController:

public class UserApiController : ApiController
{
    [HttpGet]
    public IEnumerable<ApplicationUser> GetUsers(string query = "")
    {
        using (var db = new ApplicationDbContext())
        {
            return String.IsNullOrEmpty(query) ? db.Users.ToList() :
            db.Users.Where(p => p.UserName.Contains(query)).ToList();
        }
    }
}

Controller: 控制器:

    [HttpGet]
    public ActionResult Search()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Search(ApplicationUser user)
    {
        return RedirectToAction("Details", "PrivateMessage", new { Id = user.Id });
    }

    public ActionResult Details(string Id)
    {
        using (var db = new ApplicationDbContext())
        {
            return View(db.Users.FirstOrDefault(p => p.Id == Id));
        }
    }

Model (the funky name was to rule out clashes elsewhere): 模型(时髦的名称是要排除其他地方的冲突):

public class ComposeMyMessageModel
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string NewAddress { get; set; }
}

Web page: 网页:

@model MyWebsite.Models.ComposeMyMessageModel

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

@{
  ViewBag.Title = "Compose";
}
@using (Html.BeginForm()){
  @Html.HiddenFor(model => model.Id)
  <input type="text" id="search" placeholder="Search for a product" required />
  <input type="submit" value="Go" id="submit" />
}

<script type="text/javascript">
    var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "UserApi" })';
    $('#search').autocomplete({
        source: function (request, response) {
          $.ajax({
             url: url,
             data: { query: request.term },
             dataType: 'json',
             type: 'GET',
             success: function (data) {
                 response($.map(data, function (item) {
                     return {
                         label: item.Description,
                         value: item.Id
                     }
                 }));
             }
         })
     },
      select: function (event, ui) {
         $('#search').val(ui.item.label);
         $('#Id').val(ui.item.value);
         return false;
      },
     minLength: 1
   });
</script>  

Routing: 路由:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

If I haven't pasted any code that is relevant then it's because I didn't know it was relevant! 如果我没有粘贴任何相关的代码,那是因为我不知道它是否相关! Will update accordingly. 将相应地更新。 Thanks. 谢谢。

Can you view the request and response in the browser's network tab to see what you're getting? 您可以在浏览器的“网络”标签中查看请求和响应以查看得到的内容吗? Also set a breakpoint in the success function to verify the data. 还要在成功函数中设置一个断点以验证数据。

That should point you in the right direction. 那应该为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM