c# - 选择 2 未填充 ajax 和分页
问题描述
我想用活动目录用户帐户填充选择 2 控件,以便将它们插入数据库,但即使我看到使用 F12 响应的 json 结果,选择 2 也没有填充
在共享/布局视图中引用的使用的 select2
有通过控制器的Action方法
public JsonResult GetUsersFiltredPaged(string match, int page = 1, int pageSize = 5)
{
PrincipalContext contextPrincipal = new PrincipalContext(ContextType.Domain);
var userPrincipal = new UserPrincipal(contextPrincipal);
PrincipalSearcher searchPrincipal = new PrincipalSearcher(userPrincipal);
var totalCount = searchPrincipal.FindAll().Count();
List<UserPrincipal> usersAD = new List<UserPrincipal>();
foreach (UserPrincipal users in searchPrincipal.FindAll())
{
if (users.EmployeeId != null)//prendre les comptes qui ayant un matricule paie
{
UserPrincipal userAd = users;
userAd.DisplayName = users.DisplayName + '(' + users.EmailAddress + ')';
userAd.EmployeeId = users.EmployeeId;
usersAD.Add(userAd);
}
}
IEnumerable<ModelDto> model = (from u in usersAD.AsQueryable().OrderBy(i=>i.DisplayName).Skip(page*(pageSize-1))
.Take(pageSize)
select new ModelDto{id= u.EmployeeId, text=u.DisplayName});
if (!string.IsNullOrWhiteSpace(match))
{
model = model.Where(i => i.text.Contains(match));
}
ResultList<ModelDto> results = new ResultList<ModelDto> { items = model.ToList(), totalCount = usersAD.Count };
return Json(results, JsonRequestBehavior.AllowGet);
}
我的下拉列表剃须刀
@using (Html.BeginForm("RegisterModify", "Account", FormMethod.Post, new { @class = "form-horizontal"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="well blanc" >
<div class="form-group">
@Html.LabelFor(model => model.SelectedMatric, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedMatric, Model.DisplayedNames, "--Selectionnez--", new { @class = "form-control" })
@*@Html.HiddenFor(model => model.SelectedMatric, new { id="SelectedMatric"})*@
@Html.ValidationMessageFor(model => model.SelectedMatric, "", new { @class = "text-danger" })
</div>
</div>
@*the rest of view *@
<p>
<input type="submit" value="Créer" class="btn btn-primary" />
</p>
java脚本代码
$('#SelectedMatric').select2({
placeholder: "--Selectionnez--",
width: '100%',
minimumInputLength: 0,
allowClear: true,
ajax: {
url: '@Url.Action("GetUsersFiltredPaged")',
dataType: 'json',
type: 'Get',
data: function (term, page) {
return { match: term, page: page, pageSize: 5 };
},
results: function (result, page) {
var more = (page * 5) < result.totalCount;
return { results: result.items, page: page, more: more };
}
}
});
解决方案
试试看:在 forum.asp.net
模型
public class ModelDto
{
public string id { get; set; }
public string text { get; set; }
}
public class ResultList<T>
{
public List<T> items { get; set; }
public int total_count { get; set; }
}
看法
ipt>
$(function () {
$('#SelectedMatric').select2({
placeholder: "--Selectionnez--",
width: '50%',
minimumInputLength: 0,
allowClear: true,
ajax: {
url: '@Url.Action("GetUsersFiltredPaged")',
dataType: 'json',
type: 'Get',
data: function (params) {
var query = {
match: params.term,
page: params.page || 1,
pageSize: params.pageSize || 5
}
return query;
},
processResults: function (data, params) {
console.log(params);
return {
results: data.items,
page:params.page,
pagination: {
more:(params.page * 5) < data.total_count
}
}
},
},
});
})
</script>
控制器:
public JsonResult GetUsersFiltredPaged(string match, int page = 1, int pageSize = 5)
{
List<ModelDto> model = new List<ModelDto>
{
new ModelDto{id = "1", text = "Option1" },
new ModelDto{id = "2", text = "Option2" },
new ModelDto{id = "3", text = "Option3" },
new ModelDto{id = "4", text = "Option4" },
new ModelDto{id = "5", text = "Option5" },
};
if (!string.IsNullOrWhiteSpace(match))
{
model = model.Where(m => m.text.Contains(match)).ToList();
}
ResultList<ModelDto> results = new ResultList<ModelDto>
{
items = model,
total_count = 5,
};
return Json(results, JsonRequestBehavior.AllowGet);
}
推荐阅读
- javascript - 即使已经定义了变量,所需的代码也无法找到它们
- python - 在 AppEngine 中使用烧瓶 babel 时出错
- javascript - 如何在函数中包装 Ajax 权限检查?
- laravel - 保存文件 PDF Pimaco
- mysql - mysql,从一个表2中删除表1中找到的记录
- python - Python同款网卡游戏
- ruby - 尝试使用 Ruby 创建包含时间/日期值的字符串
- c++ - 如何使用字符串文字作为宏参数?
- angular - Angular:单击独立页面上的锚标记元素
- c# - Blazor WebAssembly 401 未经授权,即使我已获得授权