C# 从数据库自动完成
大家好,我的Show All Games视图中有以下JavaScript代码:C# 从数据库自动完成,c#,javascript,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-3-areas,C#,Javascript,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 3 Areas,大家好,我的Show All Games视图中有以下JavaScript代码: <script> $('#SearchBox').autocomplete({ source: '/Controller/ShowAllGames' }); </script> 我不知道为什么我的自动完成功能不起作用,因为我键入的信息或数据库中的文字没有出现。脚本引用的搜索框也如下所示: @using (Html.BeginForm()) { <div id="Sear
<script>
$('#SearchBox').autocomplete({ source: '/Controller/ShowAllGames' });
</script>
我不知道为什么我的自动完成功能不起作用,因为我键入的信息或数据库中的文字没有出现。脚本引用的搜索框也如下所示:
@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>
}
$('#DisplaySearchResults').autocomplete({ source: '/Controller/ShowAllGames' });
我的看法是:
@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
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
@Html.Raw(" ");
@Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
}
else
{
@:Next>
@Html.Raw(" ")
@:>>
}
@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>
@model PagedList.IPagedList
@{
ViewBag.Title=“Index”;
}
@*索引*@
@使用(Html.BeginForm())
{
@TextBox(“DisplaySearchResults”,new{style=“width:420px;”})
}
@*@Html.ActionLink(“新建”、“创建”)*@
@*
配子体
游戏名
发布年份
成本
描述
下载
形象
安慰
用户名
*@
@foreach(模型中的var项目){
@*
@Html.HiddenFor(modelItem=>item.GameID)
*@
@*@DisplayFor(modelItem=>item.GameName)*@
@ActionLink(item.GameName,“Details”,新的{id=item.GameID})
@DisplayFor(modelItem=>item.ReleaseYear)
@Html.Raw(item.Description.Substring(0,item.Description.IndexOf(“.”+1))
@*@Html.DisplayFor(modelItem=>item.Description)*@
@DisplayFor(modelItem=>item.Cost)
@Html.DisplayFor(modelItem=>item.Downloads)@*希望将此作为链接,以便我可以单击它并显示游戏下载*@
@DisplayFor(modelItem=>item.ConsoleNameIDFK)
@*
@Html.HiddenFor(modeleItem=>item.UserName)
*@
@*
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/})
*@
}
@*下面是显示serach结果的页面计数和结果数的编码*@
页码@(Model.PageCount”,“Index”,新的{counterporpage=Model.PageCount,Ordering=ViewBag.CurrentSort,WordFilter=ViewBag.CurrentFilter})
}
其他的
{
@:下一步>
@Html.Raw(“”)
@:>>
}
@Format(“总计{0}个结果”,ViewBag.PageNumberResults)
(适用于@ViewBag.Search)
@*@if(ViewBag.Message!=null)
{
@查看包。留言
}
*@
var uvOptions={};
(功能(){
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);
})();
$('DisplaySearchResults').autocomplete({source:'/Controller/ShowAllGames'});
一个问题是,您正在使按钮自动完成,而不是文本框。将自动完成初始化更改为以下内容:
@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>
}
$('#DisplaySearchResults').autocomplete({ source: '/Controller/ShowAllGames' });
一个问题是,你正在使你的按钮自动完成,而不是你的文本框。将自动完成初始化更改为以下内容:
@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>
}
$('#DisplaySearchResults').autocomplete({ source: '/Controller/ShowAllGames' });
除了jrummell指出的问题外,源参数与操作的名称不匹配
<script>
$('#SearchBox').autocomplete({ source: '/ShowAllGames/AutoCompleteGames' });
</script>
在你的职责范围内。如果我没记错的话,model binder将不会按预期设置该参数。除了jrummell指出的问题之外,您的源参数与您操作的名称不匹配
<script>
$('#SearchBox').autocomplete({ source: '/ShowAllGames/AutoCompleteGames' });
</script>
在你的职责范围内。如果我没记错的话,model binder不会按预期设置该参数。您在Firebug/Chrome Developer网络选项卡中看到任何javascript或网络错误吗?在任何浏览器中没有错误您在Firebug/Chrome Developer网络选项卡中看到任何javascript或网络错误吗?在任何浏览器中都没有错误吗?键入tekken时,它不会执行任何操作,它存储在我的数据库中,自动完成没有显示它。您看到ajax请求了吗?失败了吗?插件使用ajax与您的控制器通信。当我键入tekken时,它什么也不做。tekken存储在我的数据库中。自动完成没有显示它。您看到ajax请求了吗?失败了吗?插件使用ajax与您的控制器通信。我将尝试此操作。当我键入数据库中存在的单词时,不会出现任何错误,注意出现的内容将尝试此操作,并通知您结果。谢谢我现在已试过您的代码,我无法自动完成以显示存在的任何值,我输入tek,因为数据库中存储的游戏是tekken,auto Complete不显示任何内容您添加的名称AutoComplete游戏不是控制器,而是ShowAllGames中的代码controller@user1137472自动完成需要控制器名和操作名来提交文本。如果未指定操作,路由引擎将默认为控制器的
索引
操作。我已添加了公共操作结果自动完成名称(字符串术语)在我的showgames控制器中,jrummells的答案在逻辑上似乎是正确的,但不起作用。我将尝试此操作。当我键入数据库中存在的单词时,我没有收到任何错误。注意:出现的内容将尝试此操作,并通知您结果。谢谢我现在已尝试了您的代码,但我无法自动完成以向我显示任何例如,我输入tek,因为数据库中存储的游戏是tekken,auto Complete不显示任何内容您添加的名称AutoComplete游戏不是控制器,而是ShowAl中的代码