我需要在 .net MVC 程序中使用 jQuery-ui“自动完成”,但它不起作用。任何人都可以帮助我将不胜感激。
视图代码如下:
<head runat="server">
<link href="../../Content/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
</head>
<script type="text/ecmascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Product/GetProductName/",
dataType: "json",
data: { categoryID: <%: Model.Select(o => o.Category.CategoryName).Distinct().Count() > 1 ? 0 : Model.FirstOrDefault().CategoryID %> },
contentType: "application/json; charset=utf-8",
success: function(data) {
$('#productNameSearch').autocomplete({
minLength: 0,
source: Sys.Serialization.JavaScriptSerializer.deserialize(data),
focus: function(event, ui) {
$('#productNameSearch').val(ui.item.ProductName);
return false;
},
select: function(event, ui) {
$('#productNameSearch').val(ui.item.ProductName);
$('#selectedValue').text("Selected value:" + ui.item.ProductID);
return false;
}
});
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
</script>
<% using (Html.BeginForm("ProductNameSearch", "Product", FormMethod.Post)) {%>
<input type="text" id="productNameSearch" name="productNameSearch" style="width:300px" />
<input type="submit" value="Go!" />
<div id="selectedValue"></div>
<%}%>
控制器代码如下:
[HttpGet]
public ActionResult GetProductName(int? categoryID)
{
return Json(from p in pr.GetProductList() where p.CategoryID == 2 select new { ProductID = p.ProductID, ProductName = p.ProductName }, JsonRequestBehavior.AllowGet);
}