码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-10-14 12:29:11      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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>

效果如下:

技术分享

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

标签:

原文地址:http://www.cnblogs.com/cube/p/4877000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!