我使用这个博客中的代码Autocompletion Textbox in MVC Using jQuery
但我的 jQuery 没有触发。我怀疑它与我的选择器有关。我也在使用 MVC,但我看不出这会使 javascript 有什么不同。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserManager.Models;
namespace UserManager.Controllers
{
public class UserManagerController : Controller
{
//
// GET: /UserManager/
public ActionResult Index()
{
try
{
var data = new UserManager.Models.UserManagerTestEntities();
return View(data.vw_UserManager_Model_Add_Users.ToList());
}
catch (Exception ex)
{
return View(ViewBag);
}
}
public ActionResult CreateUser()
{
var data = new UserManager.Models.UserManagerTestEntities();
ViewBag.Message = "Create New User";
return View();
}
public ActionResult LookUpGroupName(string q, int limit)
{
//TODO: Map list to autocomplete textbox control
DAL d = new DAL();
List<string> groups = d.groups();
var GroupValue = groups
.Where(x => x.StartsWith(q))
.OrderBy(x => x)
.Take(limit)
.Select(r => new { group = r });
// Return the result set as JSON
return Json(GroupValue, JsonRequestBehavior.AllowGet);
}
}
}
@model UserManager.Models.vw_UserManager_Model_Add_Users
@{
ViewBag.Title = "Create New User";
}
<h2>
CreateUser</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>New User Details</legend>
<div class="editor-label">
@Html.LabelFor(Model => Model.salutation)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.salutation)
@Html.ValidationMessageFor(model => Model.salutation)
</div>
<div class="editor-label">
@Html.LabelFor(Model => Model.firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.firstname)
@Html.ValidationMessageFor(model => Model.firstname)
</div>
<div class="editor-label">
@Html.LabelFor(Model => Model.lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.lastname)
@Html.ValidationMessageFor(model => Model.lastname)
</div>
<div class="editor-label">
@Html.LabelFor(Model => Model.password)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.password)
@Html.ValidationMessageFor(model => Model.password)
</div>
<div class="editor-label">
@Html.LabelFor(Model => Model.email)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.email)
@Html.ValidationMessageFor(model => Model.email)
</div>
<div class="editor-label">
@Html.LabelFor(Model => Model.isactive)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.isactive)
@Html.ValidationMessageFor(model => Model.isactive)
</div>
<div class="editor-label">
@Html.Label("Group Name")
<!-- GroupName -->
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.group_name)
@Html.ValidationMessageFor(model => Model.group_name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
}
<script type="text/javascript">
$(document).ready(function () {
$("#group_name").autocomplete('@Url.Action("LookUpGroupName")',
{
dataType: 'json',
parse: function (data) {
var rows = new Array();
alert("before loop");
for (var i = 0; i < data.length; i++) {
rows[i] = { data: data[i], value: data[i].group, result: data[i].group }
}
return rows;
},
formatItem: function (row, i, max) {
return row.group
},
width: 300,
highlight: false,
multiple: true,
multipleseparator: ","
});
});
</script>
呈现给浏览器的 HTML:
<input name="group_name" class="text-box single-line" id="group_name" type="text" value=""/>
可能是一些简单的东西,我只是看不到它。有任何想法吗?谢谢!