مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

mohammadd

عضویت از 1395/12/08

ساخت مدل Child , Parent برای TreeView

  • شنبه 8 شهریور 1399
  • 12:33
تشکر میکنم

سلام یه جدول دارم 

میخوام این رشترو تولید کنم کسی میتونه راهنمایی کن

dataSource: [
           { 'ID': 1, text: 'North America', children: [{ 'ID': 11, text: 'USA', children: [{ 'ID': 111, text: 'California' }, { 'ID': 112, text: 'Miami' }] }, { 'ID': 12, text: 'Canada' }, { 'ID': 13,text: 'Mexico' }] },
           { 'ID': 2, text: 'Europe', children: [{ 'ID': 21, text: 'France' }, { 'ID': 22, text: 'Spain' }, { 'ID': 23,text: 'Italy' }] },
           { 'ID': 3, text: 'South America', children: [{ 'ID': 31, text: 'Brazil' }, { 'ID': 32, text: 'Argentina' }, { 'ID': 33,text: 'Columbia' }] }
       ]

اینم مودل منوم هستش

  public class MenuViewModel
    {
        public MenuViewModel()
        {
            InverseParent = new HashSet<MenuViewModel>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public int? ParentId { get; set; }
        public int? PageCode { get; set; }
        public bool? ShowMenu { get; set; }
        public string CssClass { get; set; }
        public string Icon { get; set; }
        public string Idname { get; set; }
        public string Link { get; set; }
        public byte? Status { get; set; }
        public string HostIp { get; set; }
        public string CreateDate { get; set; }
        public string CreateTime { get; set; }
        public DateTime? CreateDateTime { get; set; }

        public virtual MenuViewModel Parent { get; set; }
        public virtual ICollection<MenuViewModel> InverseParent { get; set; }
    }

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

mohammadd

عضویت از 1395/12/08

  • سه شنبه 11 شهریور 1399
  • 21:24
 public async Task<List<TreeMenuViewModel>> GetAllMenu()
        {
            //Get All Record and Fill MenuViewModel
            IDbConnection db = new SqlConnection(_ctx.Database.GetDbConnection().ConnectionString);
            List<MenuViewModel> _List = new List<MenuViewModel>();
            _List = (await db.QueryAsync<MenuViewModel>("GetAllMenu", CommandType.StoredProcedure)).ToList();

            //Convert MenuViewModel to TreeMenuViewModel
            List<TreeMenuViewModel> FinalList = new List<TreeMenuViewModel>();
            foreach (var item in _List)
            {
                if (item.ParentId == null)
                {
                    FinalList.Add(new TreeMenuViewModel
                    {
                        ID = item.Id,
                        text = item.Title,
                        children = GetChildMenu(item.Id, _List) // Fill Child fild
                    });
                }
            }
            return FinalList;
        }

        private List<TreeMenuViewModel> GetChildMenu(int _id, List<MenuViewModel> _List)
        {
            List<TreeMenuViewModel> FinalList = new List<TreeMenuViewModel>();
            foreach (var item in _List)
            {
                if (item.ParentId == _id)
                {
                    FinalList.Add(new TreeMenuViewModel
                    {
                        ID = item.Id,
                        text = item.Title,
                        children = GetChildMenu(item.Id, _List)
                    });
                }
            }
            return FinalList;
        }

حل شد میذارم کسی عین خودم لازم داشت یک هفته الاف نشه

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)