【发布时间】:2016-09-26 07:02:17
【问题描述】:
我在我的 MVC 项目中使用Select2 Dropdown 下拉列表,如下所示。虽然我可以通过 AJAX 直接设置“查询”参数来传递它,但我无法在下面的数据部分中获取下拉列表的选定文本或值。如何获得?
@Html.DropDownListFor(x => x.StudentId, Enumerable.Empty<SelectListItem>(),
new { style ="width: 100%;" } )
$("#StudentId").select2({
multiple: false,
placeholder: "Select",
allowClear: true,
tags: true,
ajax: {
url: '/Grade/StudentLookup',
dataType: 'json',
delay: 250,
data: function (params) {
return {
//q: params.Name, // search term
//page: params.page
query : 'test' //this parameter can be passed via AJAX
//!!! but I cannot get the selected text or value of dropdownlist
};
},
processResults: function (data, page) {
var newData = [];
$.each(data, function (index, item) {
newData.push({
id: item.Id, //id part present in data
text: item.Name //string to be displayed
});
});
return { results: newData };
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 0, //for listing all, set : 0
maximumInputLength: 20, // only allow terms up to 20 characters long
});
【问题讨论】:
标签: javascript asp.net-mvc jquery-select2 select2 jquery-select2-4