JQuery 自动完成结果不出现

发布于 2024-10-07 07:47:45 字数 983 浏览 2 评论 0原文

我正在 ASP.net MVC 2 中创建一个自动完成字段。问题是输入字段只是旋转,并且没有自动完成数据或“无结果”消息出现。

我有一个搜索控制器,它使用 LIKE 语句从数据库中检索姓氏。该控制器是从下面的 JavaScript 函数调用的:

 $('#author').autocomplete('~/edit/search.mvc', {
    delay: 200,
    minChars: 2
});

这一切都工作正常。我可以调试到搜索控制器,它会传递搜索文本 (q) 并一直获取结果到 return 语句。控制器如下。

//Method to return author searches
public JsonResult Search(string q)
{
    var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
    var result = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);

    var lastNames = new List<string>();

    foreach (var facultyMember in result.FacultyMembers)
    {
        lastNames.Add(facultyMember.LastName);
    }


    return Json(lastNames, JsonRequestBehavior.AllowGet);
}

但随后在 HTML (Spark) 页面中没有出现任何结果。我有一个非常简单的文本输入:

 <input id="author" type="text" />

有什么想法吗?我无法调试超出搜索控制器末尾的任何内容,但到目前为止一切似乎都很好。

I am creating an autocomplete field in ASP.net MVC 2. The problem is the input field just spins and no autocomplete data or "no results" messages appear.

I have a Search controller that retrieves LastNames from a database using a LIKE statement. This controller is called from the JavaScript function below:

 $('#author').autocomplete('~/edit/search.mvc', {
    delay: 200,
    minChars: 2
});

This all works fine. I can debug into the Search controller and it is passing search text (q) and fetching results all the way through to the return statement. The controller is below.

//Method to return author searches
public JsonResult Search(string q)
{
    var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
    var result = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);

    var lastNames = new List<string>();

    foreach (var facultyMember in result.FacultyMembers)
    {
        lastNames.Add(facultyMember.LastName);
    }


    return Json(lastNames, JsonRequestBehavior.AllowGet);
}

But then in the HTML (Spark) page no results appear. I have a really simple Text input:

 <input id="author" type="text" />

Any ideas? I can't debug any further than the end of the Search controller, but everything seems fine up to that point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-10-14 07:47:45

我建议在 Firefox 中使用 firebug 看看返回的实际响应是什么。

I would suggest using firebug in firefox look at what the actual response coming back is.

童话 2024-10-14 07:47:45

好的,由于以下帖子和上面的评论,我明白了:

http://blogs.msdn.com/b/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery .aspx

发布生成的代码,以防对其他人有帮助。

Javascript:这会调用“编辑”控制器中的“搜索”方法(详细信息如下)。添加“Parse”函数是关键,我在上面提供的链接中对此进行了解释:

诀窍是将这些数据转换为自动完成功能期望的格式。如果您使用本地数据,自动完成功能需要一个字符串数组。由于我们的数据采用 JSON 对象的形式,因此我们将使用解析选项将 JSON 对象格式化为自动完成函数可以使用的数据。

解析函数没有很好的记录,但它基本上会获取我们的 JSON 对象并返回一个由三个强制部分组成的对象数组:

  1. 数据:这是我的 JSON 对象中的整个项目:{"ID":13,"Name":"test3","Count":1}

  2. 值:这是我想要显示的值:test3

  3. 结果:这是我从下拉列表中选择标签后要添加到输入 (txtStoryTags) 中的值:test3

完整的 Javascript 函数:

$('#author').autocomplete('~/edit/search.mvc', {
    dataType: 'json',
    parse: function(data) {
      var rows = new Array();
      for (var i = 0; i < data.length; i++) {
        rows[i] = {data: data[i], value: data[i].Name, result: data[i].Id };
     }
      return rows;
  },
  formatItem: function(row) {
     return row.Name;
  },
 delay: 40,
 autofill: true,
 selectFirst: false,
 highlight: false,
 minChars: 2
});

ASP.net MVC 2 控制器:

    //Method to return author search results as JSON
    public JsonResult Search(string q) //autocomplete passes a variable 'q' containing the search text
    {
        var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
        var authors = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);

        var results = new List<Object>();  //All we need is the name and ID
        //anonymnous object for JSON result.  
        foreach (var author in authors.FacultyMembers)
        {
            results.Add(new
            {
                Name = (author.LastName + ", " + author.FirstNames), 
                Id = author.Id
            });
        }

中提琴!自动完成功能会在搜索结果中列出用户名,选择名称后会将 ID 插入字段中,这很简单:

 <input id="author" type="text" />

谢谢大家!

OK, I figured it out thanks to the following post and the comments above:

http://blogs.msdn.com/b/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx

Posting the resulting code in case it helps someone else.

Javascript: This calls the 'Search' method in the 'Edit' controller (details below). Adding the "Parse" function was the key, which is explained in the link I provided above:

The trick is to convert this data to a format that the autocomplete function is expecting. If you are using local data, autocomplete expects an array of strings. Since our data is in the form of a JSON object, we will use the parse option to format our JSON object into data that the autocomplete function can work with.

The parse function is not documented very well but it basically will take our JSON object and return an array of objects that consist of three mandatory parts:

  1. data: this is an entire item in my JSON object: {"ID":13,"Name":"test3","Count":1}

  2. value: this is the value I want displayed: test3

  3. result: this is the value I want added to my input (txtStoryTags) after I select the tag from the dropdown: test3

The complete Javascript function:

$('#author').autocomplete('~/edit/search.mvc', {
    dataType: 'json',
    parse: function(data) {
      var rows = new Array();
      for (var i = 0; i < data.length; i++) {
        rows[i] = {data: data[i], value: data[i].Name, result: data[i].Id };
     }
      return rows;
  },
  formatItem: function(row) {
     return row.Name;
  },
 delay: 40,
 autofill: true,
 selectFirst: false,
 highlight: false,
 minChars: 2
});

The ASP.net MVC 2 Controller:

    //Method to return author search results as JSON
    public JsonResult Search(string q) //autocomplete passes a variable 'q' containing the search text
    {
        var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
        var authors = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);

        var results = new List<Object>();  //All we need is the name and ID
        //anonymnous object for JSON result.  
        foreach (var author in authors.FacultyMembers)
        {
            results.Add(new
            {
                Name = (author.LastName + ", " + author.FirstNames), 
                Id = author.Id
            });
        }

Viola! Autocomplete lists user names in the search results, and upon selecting a name inserts the ID into the field, which is simply:

 <input id="author" type="text" />

Thanks everyone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文