public ActionResult DeleteMenu(IEnumerable <MenuViewModel> models)
        {
            var datasource = new DataSource <MenuViewModel>();

            if (ModelState.IsValid)
            {
                foreach (MenuViewModel model in models)
                {
                    var menu = new Model.Domain.Menu
                    {
                        Id = model.Id
                    };
                    AutoMapper(new Models.AutoMapperParm
                    {
                        Source     = menu,
                        ObjectName = "Menus",
                        Transtype  = TransType.Delete
                    });

                    _hrUnitOfWork.MenuRepository.Remove(menu);
                }

                datasource.Errors = SaveChanges(Language);
                datasource.Total  = models.Count();
            }

            datasource.Data = models;

            if (datasource.Errors.Count() > 0)
            {
                return(Json(datasource));
            }
            else
            {
                return(Json(datasource.Data));
            }
        }
        public ActionResult DropMenuItemCopy(MenuViewModel source, MenuViewModel dest)
        {
            IList <Error> errors = null;

            if (ModelState.IsValid)
            {
                string sort;

                if (dest.Sort == null)
                {
                    sort = dest.Order.ToString().PadLeft(5, '0');
                }
                else
                {
                    sort = dest.Sort + dest.Order.ToString().PadLeft(5, '0');
                }

                if (source.MenuName != dest.MenuName)
                {
                    ////string name = source.Name;
                    ////.MenuRepository.CopyMenuAndPage(source.Name);
                    //var pages = _hrUnitOfWork.Repository<PageDiv>().Where(p => p.MenuName == source.MenuName);
                    //foreach (var page in pages)
                    //{
                    //    if (page.DivType == "Form")
                    //        _hrUnitOfWork.PagesRepository.NewCopyFormDesign(page);
                    //    else if (page.DivType == "Grid")
                    //        _hrUnitOfWork.PagesRepository.NewCopyGridDesign(page);
                    //}
                }

                //   add

                if (source.Id == 0)
                {
                    var menu = new Model.Domain.Menu
                    {
                        CompanyId  = source.CompanyId,
                        Name       = source.MenuName,
                        Order      = dest.Order,
                        NodeType   = source.NodeType,
                        ParentId   = dest.ParentId,
                        Sort       = sort,
                        Url        = source.Url,
                        Icon       = source.Icon,
                        ColumnList = source.ColumnList
                    };

                    _hrUnitOfWork.MenuRepository.Attach(menu);
                    _hrUnitOfWork.MenuRepository.Entry(menu).State = EntityState.Added;
                }
                else
                {
                    var menu = new Model.Domain.Menu
                    {
                        Id         = source.Id,
                        CompanyId  = source.CompanyId,
                        Name       = source.MenuName,
                        Order      = dest.Order,
                        NodeType   = source.NodeType,
                        ParentId   = dest.ParentId,
                        Sort       = sort,
                        Url        = source.Url,
                        Icon       = source.Icon,
                        ColumnList = source.ColumnList
                    };

                    _hrUnitOfWork.MenuRepository.Attach(menu);
                    _hrUnitOfWork.MenuRepository.Entry(menu).State = EntityState.Modified;
                }

                if (source.NodeType == 1)
                {
                    var childern = _hrUnitOfWork.MenuRepository.Find(m => m.ParentId == source.Id).ToList();
                    if (childern.Count > 0)
                    {
                        foreach (var child in childern)
                        {
                            child.Sort = sort + child.Order.ToString().PadLeft(5, '0');
                            _hrUnitOfWork.MenuRepository.Attach(child);
                            _hrUnitOfWork.MenuRepository.Entry(child).State = EntityState.Modified;
                        }
                    }
                }

                errors = SaveChanges(Language);
            }
            else
            {
                errors = Models.Utils.ParseErrors(ModelState.Values);
            }

            if (errors == null || errors.Count == 0)
            {
                return(Json("OK"));
            }
            else
            {
                return(Json(errors.FirstOrDefault().errors.FirstOrDefault().message));
            }
        }
        public ActionResult CreateMenu(IEnumerable <MenuViewModel> models)
        {
            var result     = new List <Model.Domain.Menu>();
            var datasource = new DataSource <MenuViewModel>();

            datasource.Data  = models;
            datasource.Total = models.Count();

            if (ModelState.IsValid)
            {
                if (ServerValidationEnabled)
                {
                    var errors = _hrUnitOfWork.MenuRepository.Check(new CheckParm
                    {
                        CompanyId    = CompanyId,
                        ObjectName   = "Menus",
                        ParentColumn = "CompanyId",
                        Columns      = Models.Utils.GetModifiedRows(ModelState),
                        Culture      = Language
                    });

                    if (errors.Count() > 0)
                    {
                        datasource.Errors = errors;
                        return(Json(datasource));
                    }
                }

                _hrUnitOfWork.MenuRepository.SortMenu(models);

                //  Iterate all updated rows which are posted by the PageDiv
                foreach (MenuViewModel m in models)
                {
                    // Create a new branch entity and set its properties from branchViewModel
                    var menu = new Model.Domain.Menu
                    {
                        CompanyId = CompanyId,
                        Name      = m.MenuName,
                        NodeType  = m.NodeType,
                        Order     = m.Order,
                        ParentId  = m.ParentId,
                        Sort      = m.Sort,
                        Url       = m.Url,
                        Icon      = m.Icon
                    };

                    result.Add(menu);
                    _hrUnitOfWork.MenuRepository.Add(menu);
                }

                _hrUnitOfWork.MenuRepository.UpdateTitles(models, Language);


                datasource.Errors = SaveChanges(Language);
            }
            else
            {
                datasource.Errors = Models.Utils.ParseErrors(ModelState.Values);
            }

            datasource.Data = (from m in models
                               join r in result on m.MenuName equals r.Name
                               select new MenuViewModel
            {
                Id = r.Id,
                CompanyId = m.CompanyId,
                MenuName = m.MenuName,
                NodeType = m.NodeType,
                Order = m.Order,
                ParentId = m.ParentId,
                Sort = m.Sort,
                ParentName = m.ParentName,
                Title = m.Title,
                Url = m.Url
            })
                              .ToList();


            if (datasource.Errors.Count() > 0)
            {
                return(Json(datasource));
            }
            else
            {
                return(Json(datasource.Data));
            }
        }
        public ActionResult DropMenuItem(MenuViewModel source, MenuViewModel dest)
        {
            IList <Error> errors = null;

            if (ModelState.IsValid)
            {
                string sort;

                if (dest.Sort == null)
                {
                    sort = dest.Order.ToString().PadLeft(5, '0');
                }
                else
                {
                    sort = dest.Sort + dest.Order.ToString().PadLeft(5, '0');
                }


                if (source.Id == 0)
                {
                    var menu = new Model.Domain.Menu
                    {
                        CompanyId  = source.CompanyId,
                        Name       = source.MenuName,
                        Order      = dest.Order,
                        NodeType   = source.NodeType,
                        ParentId   = dest.ParentId,
                        Sort       = sort,
                        Url        = source.Url,
                        Icon       = source.Icon,
                        ColumnList = source.ColumnList
                    };

                    _hrUnitOfWork.MenuRepository.Attach(menu);
                    _hrUnitOfWork.MenuRepository.Entry(menu).State = EntityState.Added;
                }
                else
                {
                    var menu = new Model.Domain.Menu
                    {
                        Id         = source.Id,
                        CompanyId  = source.CompanyId,
                        Name       = source.MenuName,
                        Order      = dest.Order,
                        NodeType   = source.NodeType,
                        ParentId   = dest.ParentId,
                        Sort       = sort,
                        Url        = source.Url,
                        Icon       = source.Icon,
                        ColumnList = source.ColumnList
                    };

                    _hrUnitOfWork.MenuRepository.Attach(menu);
                    _hrUnitOfWork.MenuRepository.Entry(menu).State = EntityState.Modified;
                }
                if (source.NodeType == 1)
                {
                    var childern = _hrUnitOfWork.MenuRepository.Find(m => m.ParentId == source.Id).ToList();
                    if (childern.Count > 0)
                    {
                        foreach (var child in childern)
                        {
                            child.Sort = sort + child.Order.ToString().PadLeft(5, '0');
                            _hrUnitOfWork.MenuRepository.Attach(child);
                            _hrUnitOfWork.MenuRepository.Entry(child).State = EntityState.Modified;
                        }
                    }
                }

                errors = SaveChanges(Language);
            }
            else
            {
                errors = Models.Utils.ParseErrors(ModelState.Values);
            }

            if (errors == null || errors.Count == 0)
            {
                return(Json("OK"));
            }
            else
            {
                return(Json(errors.FirstOrDefault().errors.FirstOrDefault().message));
            }
        }