2012-04-09 111 views
0

人嗨,我有以下的JavaScript代码在我的显示所有游戏查看:自动完成从数据库

<script> 
    $('#SearchBox').autocomplete({ source: '/Controller/ShowAllGames' }); 
</script> 

和下面的代码的功能我自动完成我的ShowAllGames控制器:

public ActionResult AutoCompleteGames(string term) 
    { 
     var db = new gamezoneDBEntities(); 
     return Json(db.tblGames.Where(games => games.GameName.StartsWith(term)).Select(games => games.GameName), JsonRequestBehavior.AllowGet); 

    } 

我不不知道为什么我的自动填充不起作用,因为我在我的数据库中键入我的信息或单词没有出现。此外,该脚本指的搜索框如下:

@using (Html.BeginForm()) 
{ 
    <div id="SearchBorder"> 
    <div id="TopSearch"> 

     @Html.TextBox("DisplaySearchResults", "", new { style = "width:420px;" }) 
     <input id="SearchBox" type="submit" value="Search news archives"/> 
     </div> 
     </div> 
} 

我有一个检索算法框和页面启用了所有的工作也只是想知道为什么我的自动完成不工作

谢谢

如果需要addtional信息,请问我西港岛线提供的感谢

编辑:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using PagedList; 
using GamesTest.Models; 


namespace GamesTest.Controllers 
{ 
    public class ShowAllGamesController : Controller 
    { 
     // 
     // GET: /ShowAllGames/ 

     public ActionResult Index(string Ordering, string WordFilter, string DisplaySearchResults, int? CounterForPage) 
     { 
      using (var db = new gamezoneDBEntities()) 
      { 

       ViewBag.Message = TempData["message"]; 
       ViewBag.CurrentSort = Ordering; 
       ViewBag.NameSortParm = String.IsNullOrEmpty(Ordering) ? "GameName" : ""; 
       ViewBag.DateSortParm = Ordering == "ReleaseYearOfGame" ? "DiscriptionOfGame" : "Date"; 


       { 
        TempData["DisplaySearchResult"] = DisplaySearchResults; 

        { 
         ViewBag.search = DisplaySearchResults; 
        } 
        if (Request.HttpMethod == "GET") 
        { 
         DisplaySearchResults = WordFilter; 
        } 
        else if (DisplaySearchResults == "") 
        { 
         ViewData["MyMessage"] = "Nothing Has Been Entered."; 

        } 

        else 
        { 
         CounterForPage = 1; 
        } 

        ViewBag.CurrentFilter = DisplaySearchResults; 

        var FullDatabaseItem = from b in db.tblGames 
              select b; 
        if (!String.IsNullOrEmpty(DisplaySearchResults)) 
        { 
         FullDatabaseItem = FullDatabaseItem.Where(b => b.GameName.ToUpper().Contains(DisplaySearchResults.ToUpper())); 

        } 
        switch (Ordering) 
        { 
         case "HeadlineName": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.GameName); 
          break; 
         case "DatePosted": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear); 
          break; 
         case "DiscriptionDate": 
          FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear); 
          break; 
         default: 
          FullDatabaseItem = FullDatabaseItem.OrderByDescending(b => b.ReleaseYear); 
          break; 
        } 

        int pageSize = 3; 
        int pageNumber = (CounterForPage ?? 1); 
        var PageNumberResults = FullDatabaseItem.ToPagedList(pageNumber, pageSize); 
        ViewBag.PageNumberResults = FullDatabaseItem.Count(); 
        if (PageNumberResults.Any()) 
        { 

         return View(PageNumberResults); 
        } 

        return View("ErrorView"); 
       } 
      } 
     } 


     public ActionResult AutoCompleteGames() 
     { 
      var db = new gamezoneDBEntities(); 
      string term = this.Request.Params["term"].ToString(); 
      return Json(db.tblGames.Where(games => games.GameName.StartsWith(term)).Select(games => games.GameName), JsonRequestBehavior.AllowGet); 

     } 

     // 
     // GET: /ShowAllGames/Details/5 


     public ViewResult Details(int id) 
     { 
      using (var db = new gamezoneDBEntities()) 
      { 
       tblGame tblgame = db.tblGames.Find(id); 
       return View(tblgame); 
      } 
     } 

     // 
     // GET: /ShowAllGames/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllGames/Create 

     [HttpPost] 
     public ActionResult Create(FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add insert logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /ShowAllGames/Edit/5 

     public ActionResult Edit(int id) 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllGames/Edit/5 

     [HttpPost] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add update logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /ShowAllGames/Delete/5 

     public ActionResult Delete(int id) 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllGames/Delete/5 

     [HttpPost] 
     public ActionResult Delete(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add delete logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 
    } 
} 

笔者认为:

@model PagedList.IPagedList<GamesTest.tblGame> 

@{ 
    ViewBag.Title = "Index"; 
} 

@*<h2>Index</h2>*@ 

@using (Html.BeginForm()) 
{ 
    <div id="SearchBorder"> 
    <div id="TopSearch"> 

     @Html.TextBox("DisplaySearchResults", "", new { style = "width:420px;" }) 
     <input id="SearchBox" type="submit" value="Search news archives"/> 
     </div> 
     </div> 
} 


<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery.ui.autocomplete.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery.ui.position.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery.ui.core.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery.ui.widget.js" type="text/javascript"></script> 

<p> 
@* @Html.ActionLink("Create New", "Create")*@ 
</p> 
<table id = "OverAll"> 
@* <tr> 
     <th> 
      GameID 
     </th> 
     <th> 
      GameName 
     </th> 
     <th> 
      ReleaseYear 
     </th> 
     <th> 
      Cost 
     </th> 
     <th> 
      Description 
     </th> 
     <th> 
      Downloads 
     </th> 
     <th> 
      Image 
     </th> 
     <th> 
      Console 
     </th> 
     <th> 
      UserName 
     </th> 
     <th></th> 
    </tr>*@ 

@foreach (var item in Model) { 
    <tr> 
    @* <td> 
      @Html.HiddenFor(modelItem => item.GameID) 
     </td>*@ 

     <td id = "TableLayout1"> 
      <img width="100" height="100"alt="ImageFromDatabase" src='@item.Image' /> 
     </td> 
     <td id = "TableLayout2"> 
      @*@Html.DisplayFor(modelItem => item.GameName)*@ 
      @Html.ActionLink(item.GameName, "Details", new { id = item.GameID }) 
     </td> 

     <td id = "TableLayout3"> 
      @Html.DisplayFor(modelItem => item.ReleaseYear) 
     </td> 
     <td id = "TableLayout4"> 
      @Html.Raw(item.Description.Substring(0, item.Description.IndexOf(".") + 1)) 
      @* @Html.DisplayFor(modelItem => item.Description)*@ 
     </td> 
     <td id = "TableLayout5"> 
      @Html.DisplayFor(modelItem => item.Cost) 
     </td> 

     <td id = "TableLayout6"> 
      @Html.DisplayFor(modelItem => item.Downloads) @*want this as a link so I can then click on it and show the game downloads*@ 

     </td> 

     <td id = "TableLayout7"> 
      @Html.DisplayFor(modelItem => item.ConsoleNameIDFK) 
     </td> 
     @*<td> 
      @Html.HiddenFor(modelItem => item.UserName) 
     </td>*@ 
    @* <td> 
      @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
     </td>*@ 
    </tr> 
} 

</table> 
@*Below is coding for the page count and the number of results found with the serach result displayed*@ 

<div class="PageCounter"> 
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) 
    of @Model.PageCount 
    &nbsp; 
    @if (Model.HasPreviousPage) 
    { 

     @Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter }) 
    } 
    else 
    { 
     @:<< 
     @Html.Raw("&nbsp;"); 
     @:< Prev 
    } 
    &nbsp; 
    @if (Model.HasNextPage) 
    { 
     @Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter }) 
    } 
    else 
    { 
     @:Next> 
     @Html.Raw("&nbsp;") 
     @:>> 
    } 

    @String.Format("Total of {0} results", ViewBag.PageNumberResults) 
    (For @ViewBag.Search) 


@* @if(ViewBag.Message != null) 
{ 
    <p>@ViewBag.Message</p> 
} 
*@ 


</div> 


<script type="text/javascript"> 
    var uvOptions = {}; 
    (function() { 
     var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true; 
     uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/ZRhsC1RL1m4gK5megTxxlw.js'; 
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s); 
    })(); 
</script> 


<script type="text/javascript"> 
    $('#DisplaySearchResults').autocomplete({ source: '/Controller/ShowAllGames' }); 
</script> 
+0

您是否在Firebug/Chrome Developer Network标签中看到任何javascript或网络错误? – jrummell 2012-04-09 16:56:11

+0

没有在任何浏览器中没有错误 – user1137472 2012-04-09 16:57:06

回答

1

除了问题jrummell指出,源参数不匹配您的操作的名称。

<script> 
    $('#SearchBox').autocomplete({ source: '/ShowAllGames/AutoCompleteGames' }); 
</script> 

我怀疑当您在搜索框中输入时出现404错误。

编辑

那么它没有任何意义,我认为你没有得到404的,但试试这个;从您的动作中删除参数string term并在您的功能中使用

string term = this.Request.Params["term"].ToString(); 

。如果我没有记错,模型联编程序将不会按预期设置该参数。

+0

我会尝试这个我没有得到当我键入数据库注意到出现exsits会尝试这样的一个通知你结果的感谢 – user1137472 2012-04-09 17:32:59

+0

的话任何错误INFACT我现在已经尝试过你的代码,我不能自动完成向我展示任何存在的值,因为存储在数据库中的游戏是tekken,所以我键入tek,自动完成并不显示任何内容 – user1137472 2012-04-09 17:37:12

+0

您添加的名称AutoCompleaseGames不是控制器它是ShowAllGames控制器 – user1137472 2012-04-09 17:38:40

3

一个问题是,你正在你的按钮自动完成,而不是你的文本框的。您自动完成初始化更改为以下:

$('#DisplaySearchResults').autocomplete({ source: '/Controller/ShowAllGames' }); 
+0

我会尝试这 – user1137472 2012-04-09 17:01:17

+0

它什么都不做,当我键入存储在我的数据库中的tekken自动补全不显示它 – user1137472 2012-04-09 17:02:56

+0

您是否看到一个ajax请求?它失败了吗? – jrummell 2012-04-09 17:03:45