- private string ColumnNav(int parentId=0,int levelId=1)
- {
-
-
- int siteId = Html.CurrentSiteId();//获取当前站点Id
- IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
- columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
- columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
- if (columnList.Count()==0)
- {
- return ""; //没有子集则返回空字符串
- }
- if (levelId == 1) {
- string html = "<ul class=\"nav-list\">";
- }else {
- string html = "<ul class=\"menu\">";
- }
-
- foreach (dynamic item in columnList)
- {
- int columnId = item.Id;//调用栏目id字段
- string name = item.Name; //调用栏目Name字段
- string url = Html.ColumnUrl(columnId);//获取栏目url地址
- int level = item.Level;//调用栏目level字段
- //自定义拼接开始
- html += "<li>";
- html += "<a href=\"" + url + "\">" + name + "</a>";
- html += ColumnNav(columnId,level); //递归获取
- html += "</li>";
- }
- html += "</ul>";
- return html;
- }
复制代码上述代码是自定义导航css样式属性值,根据level值的去判断是否是二级菜单。但是,前端都是levelId == 1这条语句的代码块。
通过以下两个函数,就可以实现,但是两个函数的代码重复率很高,能不能在一个函数实现。
- private string ColumnNav(int parentId=0)
- {
-
-
- int siteId = Html.CurrentSiteId();//获取当前站点Id
- IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
- columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
- columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
- if (columnList.Count()==0)
- {
- return ""; //没有子集则返回空字符串
- }
- string html = "<ul class=\"nav-list\">";
-
- foreach (dynamic item in columnList)
- {
- int columnId = item.Id;//调用栏目id字段
- string name = item.Name; //调用栏目Name字段
- string url = Html.ColumnUrl(columnId);//获取栏目url地址
- int level = item.Level;//调用栏目level字段
- //自定义拼接开始
- html += "<li>";
- html += "<a href=\"" + url + "\">" + name + "</a>";
- html += SecondaryMenu(columnId); //递归获取
- html += "</li>";
- }
- html += "</ul>";
- return html;
- }
- private string SecondaryMenu(int parentId)
- {
- int siteId = Html.CurrentSiteId();//获取当前站点Id
- IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
- columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
- columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
- if (columnList.Count()==0)
- {
- return ""; //没有子集则返回空字符串
- }
- string html = "<ul class=\"menu\">";
- foreach (dynamic item in columnList)
- {
- int columnId = item.Id;//调用栏目id字段
- string name = item.Name; //调用栏目Name字段
- string url = Html.ColumnUrl(columnId);//获取栏目url地址
- int level = item.Level;//调用栏目level字段
- //自定义拼接开始
- html += "<li>";
- html += "<a href=\"" + url + "\">" + name + "</a>";
- html += "</li>";
- }
- html += "</ul>";
- return html;
- }
复制代码