• ASP.NET MVC Select无限级分类选择下拉框


    1:读取父级下的所有子类别 

    *ViewBag.ParentItemList:不能与ParentId相同
     private void ParentDropDownList()
            {
                List<SAS.Model.BPMS_SysMenu> list = new List<SAS.Model.BPMS_SysMenu>();
                StringBuilder strWhere = new StringBuilder();
                List<SelectListItem> selectList = new List<SelectListItem>();
                list = bll.GetModelList(strWhere.ToString());
                foreach (var item in list)
                {
                    if (item.ParentId.Equals("0"))
                    {
                        var parentList = bll.GetModelList(strWhere.ToString()).Where(d => d.ParentId == item.MenuId);
                        foreach (var plitem in parentList)
                        {
                            string parentName = plitem.FullName;
                            string parentId = plitem.MenuId.ToString();
                            selectList.Add(new SelectListItem
                            {
                                Text = "├-" + parentName,
                                Value = parentId
                            });
                            string pid = parentId;
                            string tag = " |- ";
                            ChildrenList(pid, tag, selectList);
                        }
                        ViewBag.ParentItemList = new SelectList(selectList, "Value", "Text");
                    }
                }
                
            }

    2:遍历读取所有子类别下的孙类别。

         private void ChildrenList(string pid, string tag, List<SelectListItem> selectList)
            {
                StringBuilder strWhere = new StringBuilder();
                var childrenList = bll.GetModelList(strWhere.ToString()).Where(d => d.ParentId == pid);
                foreach (var clitem in childrenList)
                {
                    string clname = tag + clitem.FullName;
                    string clid = clitem.MenuId.ToString();
                    selectList.Add(new SelectListItem
                    {
                        Text = clname,
                        Value = clid
                    });
                    string pid2 = clid;
                    string tag2 = tag + " -- ";
                    ChildrenList(pid2, tag2, selectList);
                }
            }

    3:在需要的地方调用父类方法

    public ActionResult Edit(string id)
            {
                ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
                var model = new SAS.Model.BPMS_SysMenu();
                model=bll.GetModel(id);
                if (model != null)
                {
                    ParentDropDownList();  
                    return View(model);
                }
                else
                {
                    return View("404");
                }
            }

    4:在页面调用

    <div class="form-group">
                        <label class="col-md-3 control-label">上级模块</label>
                        <div class="col-md-4">
           @Html.DropDownListFor(model => model.ParentId, ViewBag.ParentItemList as IEnumerable<SelectListItem>, new { @class = "form-control" })
                            <span class="help-block">
                            </span>
                        </div>
                    </div>

    效果如下:

  • 相关阅读:
    angularjs+ionic的app端分页和条件
    js中对象的自定义排序
    angularJS入门笔记
    SpringBoot学习历程
    页面渲染流程
    跨域问题
    jquery知识点结合使用
    对bootstrap模态框的小尝试
    登录页面两端对齐的样式问题
    输入两个数字,将比较结果输出到页面
  • 原文地址:https://www.cnblogs.com/cube/p/4877000.html
Copyright © 2020-2023  润新知