【发布时间】:2015-09-28 13:54:36
【问题描述】:
当我在 select2 文本框中键入或搜索任何内容时,它会显示搜索,但不会显示数据库中的数据。 我在 searchProdauto 上放了断点,当编译器启动它时,
searchData 动作搜索数据成功,但它没有显示在 select2 文本框中..
这是我的家庭控制器
// GET: /Common/Home/
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult searchProdauto(string q)
{
var searchData = rep.GetAll().Where(x => x.ProductName.StartsWith(q,
StringComparison.InvariantCultureIgnoreCase)).Select(y =>
y.ProductName).ToList();
return Json(searchData, JsonRequestBehavior.AllowGet);
}
Index.cshtml查看代码
@using Medi.Modals
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/metro.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>
<link href="~/Content/metro.css" rel="stylesheet" />
<link href="~/Content/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<br />
<div class="cell auto-size padding20" style="margin-top:1rem;">
<h2>Select2 Examples</h2>
<h3>Basic</h3>
<select class="js-data-example-ajax">
<option value="3620194" selected="selected">select2/select2</option>
</select>
<br />
</div>
<script type="text/javascript">
$(".js-data-example-ajax").select2({
ajax: {
url: '@Url.Action("searchProdauto","Home")',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data.items
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
});
</script>
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-5 jquery-select2