【问题标题】:ASP.NET MVC jquery autocomplete with value and text field带有值和文本字段的 ASP.NET MVC jquery 自动完成
【发布时间】:2012-09-20 11:08:07
【问题描述】:

控制器

public ActionResult Search(string id)
{
     id= Request.QueryString["term"];         
     var routeList = db.Movies.Where(r => r.Title.Contains(id))
                   .Take(5)
                   .Select(r => new { id = r.MovieID, label = r.Title, name = "MovieID" });
     return Json(routeList, JsonRequestBehavior.AllowGet);
}

查看:

<input type="hidden"   id="MovieID"  name="MovieID" />
<input type="text" id="SelectedMovie" value=""/>
<script type="text/javascript" language="javascript">
   $("#SelectedMovie").autocomplete({
       source: function (request, response) {
           $.ajax({
              url: "/Transaction/Search", type: "POST", dataType: "json",                        
              data: { id: request.term }, 
              success: function (data) {
              response($.map(data, function (item) {                                
                return { label: item.label, value: item.id }; //updated code
               }));
             }
         });
     },
     select: function (event, ui) {
         $("#MovieID").val(ui.item.value);
         $("#SelectedMovie").val(ui.item.label);
         return false;
     }
  });
</script>

我有某种视频商店应用程序。当我去租一部电影时,我需要一个包含电影的组合框,我可以使用自动完成来选择它。 还要求只有 ID(值)保存到数据库中,而不是文本本身。

编辑:这是完整的工作示例

【问题讨论】:

  • 第一个请告诉我我的控制器是否正常?当我手动运行它时,我没有得到任何结果,搜索词(id)被传递为 null ??
  • 我希望通过 request.term NOT request.id$.ajax() 调用。

标签: jquery asp.net-mvc jquery-ui-autocomplete


【解决方案1】:

由于您只向服务器端的Search() 函数传递一个字符串,因此您通过$.ajax() 调用传递的data 元素需要更改。

public ActionResult Search(string id)//I think that the id that you are passing here needs to be the search term. You may not have to change anything here, but you do in the $.ajax() call
{
      id= Request.QueryString["term"];

      var routeList = db.Movies.Where(r => r.Title.Contains(id))//this is a text filter no?
                        .Take(5)
                        .Select(r => new { id = r.MovieID, label = r.Title, name = "MovieID" });
      return Json(routeList, JsonRequestBehavior.AllowGet);
}

$("#MovieID").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Transaction/Search", type: "POST", dataType: "json",
            //original code
            //data: { searchText: request.id, maxResults: 10 },
            //updated code; updated to request.term 
            //and removed the maxResults since you are not using it on the server side
            data: { id: request.term },

            success: function (data) {
                response($.map(data, function (item) {
                    //original code
                    //return { label: item.FullName, value: item.FullName, id: item.TagId }; 
                    //updated code
                    return { label: item.label, value: item.label, id: item.id };
                }));
            },
        select: function (event, ui) {
                //update the jQuery selector here to your target hidden field
            $("input[type=hidden]").val(ui.item.id);
        }
        });
    },
});

让我知道这是否有效/有帮助!

【讨论】:

  • 好的,这行得通。但我需要将其保存在隐藏的字段中,而不是在同一字段中显示 ID。请帮忙
  • @Jalle,我的答案已更新以反映您需要的更改。让我知道这是否有帮助!
  • @JoeFletch,一切都按预期工作,除了当我选择下拉值时没有触发事件。我已将调试器放在'select:function(event,ui){'中,但编译器从未到达那里。您对可能出现的问题有任何想法吗?
  • @LuisGouveia 没有线索。我没有发布你的任何代码,所以我不知道你的情况。
  • @JoeFletch 谢谢你,但在我发表评论几天后我自己想通了。很抱歉没有告诉你。
【解决方案2】:

您的.ajax() 调用未在id 中指定。它不在您的 data{} 对象中,也不在 querystring 中作为 url 参数的一部分(任何一种方法都可以)。

因此您的 Action 方法中的 null 值。

无论如何,您会立即用Request.QueryString["term"] 覆盖方法的id 参数。为什么要这么做??

而不是询问方法中的“术语”,您只需 将其作为参数本身绑定到Action 方法,如下所示:

public ActionResult Search(string term)
{
    var routeList = db.Movies.Where(r => r.Title.Contains(term))
            .Take(5)
            .Select(r => new { id = r.MovieID, label = r.Title, name = "MovieID" });
    return Json(routeList, JsonRequestBehavior.AllowGet);
}

【讨论】:

    【解决方案3】:

    首先,您应该使用函数的以下返回值:

    return { label: item.title, value: item.id };
    

    根据documentation,您必须返回具有labelvalue 属性的对象(没有id 属性)。标签是用户看到的,值是发布到服务器的。

    其次,您在 Ajax 调用中传递了 searchTextmaxResults,因此您的操作方法应该有两个参数:public ActionResult Search(string searchText, int maxResults)

    您能否应用这些更改并查看它是否有效?

    【讨论】:

    • 甚至没有注意到那个。当属性名称不同时,它肯定是行不通的。
    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多