17

控制器

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(值)保存到数据库中,而不是文本本身。

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

4

3 回答 3

20

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

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

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

于 2012-09-20T12:41:19.573 回答
4

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

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

无论如何,您会立即用Request.QueryString["term"]. 为什么要这样做??

无需在方法中询问“术语” ,您只需将其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);
}
于 2012-09-20T11:51:48.257 回答
3

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

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

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

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

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

于 2012-09-20T11:17:48.300 回答