我想在输入信件时自动完成。 我有一个数据库“USERS”,它有名字。当我尝试发短信,例如e 它必须显示“埃德加,埃德温,埃米尔”,但是,什么都不显示。 ClientController在这里:
public class ClientController : Controller
{
public JsonResult AutocompleteSuggestions(string searchstring)
{
ModelContext db = new ModelContext();
var suggestions = from E in db.USERS
select E.Name;
var namelist = suggestions.Where(n => n.ToLower().Contains(searchstring.ToLower()));
return Json(namelist, JsonRequestBehavior.AllowGet);
}
}
index.cshtml这里:在这里有一个文本框,我发送客户端控制器autocopleteSuggeston方法,但它没有去或它不工作。我在cshtml上添加jquery脚本文件,但它仍然无法正常工作。
@using (Html.BeginForm())
{
<p>
Name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<script type="text/javascript">
window.jQuery(function () {
window.jQuery("#SearchString").autocomplete({
source: "/Client/AutocompleteSuggestions",
minLength: 1,
select: function (event, ui) {
if (ui.item) {
window.jQuery("#SearchString").val(ui.item.value);
window.jQuery("form").submit();
}
}
});
});
</script>
我添加jquery 错误在哪里?
答案 0 :(得分:1)
你需要在JsonResult方法之前添加[HttpPost],如下所示:
[HttpPost]
public JsonResult AutocompleteSuggestions(string searchstring)
{
ModelContext db = new ModelContext();
var suggestions = from E in db.USERS
select E.Name;
var namelist = suggestions.Where(n => n.ToLower().Contains(searchstring.ToLower()));
return Json(namelist, JsonRequestBehavior.AllowGet);
}
因为此处的表单是使用 window.jQuery(&#34; form&#34;)。submit()提交的,所以它会调用Post Action,因此您需要添加[HttpPost]捕获表单提交或任何类型的Post Action!
将您的观看代码更改为
@using( Html.BeginForm(null, null, FormMethod.Post, new{@id ="SearchForm"} ))
{
<p>
Name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
&#13;
$(function() {
$("#SearchString").autocomplete({
source: "/Client/AutocompleteSuggestions",
select: function(event, ui) {
$("#SearchString").val(ui.item.value);
$("#SearchForm").submit();
}
});
});