Model类
public class MenuModel
{ public int Id { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string Icon { get; set; }
public DateTime CreateDate { get; set; }
public MenuModel Parent { get; set; }
public int SortId { get; set; }
public bool IsDisabled { get; set; }
public int? ParentId { get; set; }
public string Buttoms { get; set; }
public virtual List<MenuModel> Childs { get; set; }
}
//方法
//获取所有菜单权限
public static List<Model.MenuModel> GetUserMenu()
{
using (var db = new MLContext())
{
var list = new List<Model.MenuModel>();
var menus = db.SYS_Menu.Where(m => m.IsDisabled == false).OrderBy(m => m.Id).ToList();
if (menus == null) return null;
foreach (var poco in menus.Where(m => !m.ParentId.HasValue).OrderBy(m => m.Id))
{
var model = ConvertHelper.ToMenuModel(poco); //父级
list.Add(model);
var childs = menus.Where(m => m.ParentId == poco.Id).OrderBy(m => m.Id).ToList();
if (childs == null || childs.Count == 0) continue;
model.Childs = new List<Model.MenuModel>();
foreach (var cpoco in childs)
{
var child = ConvertHelper.ToMenuModel(cpoco); //子级
child.Parent = model;
model.Childs.Add(child);
}
}
return list;
}
}