【问题标题】:Select 2 not populated with ajax and pagination选择 2 未填充 ajax 和分页
【发布时间】:2019-10-14 10:34:27
【问题描述】:

我想用活动目录用户帐户填充选择 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 };
                }
            }
        });

【问题讨论】:

    标签: c# asp.net-mvc jquery-select2


    【解决方案1】:

    试试看:in 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多