.net layui mvc EF xm-select 多选下拉框 动态赋值 用于列表多选查询

首先得引用js ,(js也文件可以自己去下载,也可以点下面链接下载https://download.csdn.net/download/Tenderness104/85585086)

 

然后,前台放一个这个

 

js里这样写

后台

ajax里动态赋值的方法

  public JsonResult GetCorp1()

        {
            using (ModelsContainer1 db = new ModelsContainer1())

            {
                db.Configuration.ProxyCreationEnabled = false;//关闭EF的默认加载


                var queryAll = db.XH_CorpInfo.ToList();

                var query = queryAll.Where(u => true);//定义list变量进行条件查询

                //判断属于哪个账号
                XH_LoginUser loginuser = (XH_LoginUser)Session["user"];
                XH_LoginUser corp = db.XH_LoginUser.Where(d => d.IsDel == 0 && d.ID == loginuser.ID).FirstOrDefault();
                if (loginuser.ID != 1)
                {
                    if (!string.IsNullOrWhiteSpace(corp.CorpId))
                    {
                        int cid = 0;
                        string corpid = corp.CorpId;
                        List list = new List(corpid.Split(','));
                        List list1 = new List();

                        foreach (var item in list)
                        {
                            cid = Convert.ToInt32(item.ToString());
                            list1.Add(cid);
                        }

                        query = query.Where(u => list1.Contains(u.CorpId));

                    }
                }
                query = query.Where(u => u.IsDel != 1);
                var pageQuery = new List();

                if (query != null && query.Any())
                {
                    pageQuery = query.ToList();
                }

                List HuChiList = new List();

                StringBuilder str = new StringBuilder();
                //将数据Json化并传到前台视图
                foreach (var item in pageQuery)
                {
                    huchi1 aaa = new huchi1();
                    aaa.value = item.CorpId;
                    aaa.name = item.CorpName;

                    HuChiList.Add(aaa);

                    //str.Append("{");
                    //str.Append("id:"+ item.PlanId + ",");
                    //str.Append("name:\"" + item.PlanName + "\"");
                    //str.Append("},");

                }

                var json1 = Json(HuChiList, JsonRequestBehavior.AllowGet);
                return json1;
                //var serJson = "["+ str.ToString()+"]";


                //return Json(serJson, JsonRequestBehavior.AllowGet);

            }

        }

后台,查询列表方法

public ActionResult GetAjaxplanPageList(int page, int limit, string keyWord, string select,string dxcorp)
        {
            using (ModelsContainer1 entities = new ModelsContainer1())
            {
                var queryAll = entities.XH_CorpPlanInfo.ToList();

                var query = queryAll.Where(u => true);//定义list变量进行条件查询

                //按名称查询
                if (!string.IsNullOrWhiteSpace(keyWord))
                {
                    keyWord = keyWord.Trim();
                    //query = query.Where(u => u.Title.Contains(keyWord));
                    query = query.Where(u => string.IsNullOrWhiteSpace(u.Title) ? false : u.Title.Contains(keyWord));


                }
               
                //按公司多选查询
                if (!string.IsNullOrWhiteSpace(dxcorp))
                {
                   
                        int cid = 0;
                        string corpid = dxcorp;
                        List list = new List(corpid.Split(','));
                        List list1 = new List();

                        foreach (var item in list)
                        {
                            cid = Convert.ToInt32(item.ToString());
                            list1.Add(cid);
                        }

                        query = query.Where(u => list1.Contains(u.CorpId));

                    

                }



                //判断属于哪个账号
                XH_LoginUser loginuser = (XH_LoginUser)Session["user"];
                XH_LoginUser corp = entities.XH_LoginUser.Where(d => d.IsDel == 0 && d.ID == loginuser.ID).FirstOrDefault();
                if (loginuser.ID != 1)
                {
                    if (!string.IsNullOrWhiteSpace(corp.CorpId))
                    {
                        int cid = 0;
                        string corpid = corp.CorpId;
                        List list = new List(corpid.Split(','));
                        List list1 = new List();

                        foreach (var item in list)
                        {
                            cid = Convert.ToInt32(item.ToString());
                            list1.Add(cid);
                        }

                        query = query.Where(u => list1.Contains(u.CorpId));

                    }
                }

                //判断不是已删除数据
                query = query.Where(u => u.IsDel != 1);

                var pageQuery = new List();

                if (query != null && query.Any())
                {
                    pageQuery = query.OrderByDescending(a => a.Id).Skip(limit * (page - 1)).Take(limit).ToList();
                }

                var result = new
                {
                    code = 0,
                    msg = "",
                    count = query.Count(),
                    data = pageQuery
                };
                return Json(result, JsonRequestBehavior.AllowGet);

            }
        }

ok

效果图

.net layui mvc EF xm-select 多选下拉框 动态赋值 用于列表多选查询_第1张图片

.net layui mvc EF xm-select 多选下拉框 动态赋值 用于列表多选查询_第2张图片

你可能感兴趣的:(layui,linq,前端,mvc,javascript)