public ActionResult Edit(MenuModulViewModel menuModulViewModel, int pastDisOrder, int pastPosition)
        {
            if (ModelState.IsValid)
            {
                Module module = _moduleService.GetById(menuModulViewModel.ModuleId);
                if (module != null)
                {
                    module.ModuleTitle  = menuModulViewModel.ModuleTitle;
                    module.PositionId   = (int)menuModulViewModel.PositionId;
                    module.IsActive     = menuModulViewModel.IsActive;
                    module.Accisibility = menuModulViewModel.Accisibility;
                    module.DisplayOrder = menuModulViewModel.DisplayOrder;


                    //Method for selecting menus for modules
                    List <ModulePage> modulePageAddList    = new List <ModulePage>();
                    List <ModulePage> modulePageRemoveList = new List <ModulePage>();
                    foreach (var item in _menuService.menus())
                    {
                        if (Request.Form["Page[" + item.MenuId.ToString() + "]"] != null && !(_modulePageService.ExistModulePage(menuModulViewModel.ModuleId, item.MenuId)))
                        {
                            ModulePage modulePage = new ModulePage()
                            {
                                MenuId   = item.MenuId,
                                ModuleId = menuModulViewModel.ModuleId,
                            };
                            modulePageAddList.Add(modulePage);
                        }
                        else if (Request.Form["Page[" + item.MenuId.ToString() + "]"] == null && _modulePageService.ExistModulePage(menuModulViewModel.ModuleId, item.MenuId))
                        {
                            ModulePage PageRemove = _modulePageService.GetByMenuModule(menuModulViewModel.ModuleId, item.MenuId);
                            modulePageRemoveList.Add(PageRemove);
                        }
                    }
                    _modulePageService.Add(modulePageAddList);
                    _modulePageService.Delete(modulePageRemoveList);

                    //editing MenuModule GroupId if it's changed
                    var EditMenuModule = _menuModuleService.GetByModuleId(menuModulViewModel.ModuleId);
                    if (EditMenuModule.MenuGroupId != menuModulViewModel.MenuGroupId)
                    {
                        EditMenuModule.MenuGroupId = menuModulViewModel.MenuGroupId;
                        _menuModuleService.Edit(EditMenuModule);
                    }

                    //Editing the Module
                    _moduleService.Edit(module, pastPosition, pastDisOrder);
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "خطایی وجود دارد");
                ViewBag.PositionId  = new SelectList(_moduleService.Positions(), "PositionId", "PositionTitle");
                ViewBag.MenuGroupId = new SelectList(_menuGroupService.MenuGroup(), "MenuGroupId", "MenuTitile");
                return(View());
            }
            return(RedirectToAction("Index"));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(MenuModulViewModel menuModulViewModel, int pastDisOrder, int pastPosition)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Module module = await _moduleService.GetById(menuModulViewModel.ModuleId);

                    if (module != null)
                    {
                        module.ModuleTitle  = menuModulViewModel.ModuleTitle;
                        module.PositionId   = (int)menuModulViewModel.PositionId;
                        module.IsActive     = menuModulViewModel.IsActive;
                        module.Accisibility = menuModulViewModel.Accisibility;
                        module.DisplayOrder = menuModulViewModel.DisplayOrder;


                        //Method for selecting menus for modules
                        List <ModulePage> modulePageAddList    = new List <ModulePage>();
                        List <ModulePage> modulePageRemoveList = new List <ModulePage>();
                        foreach (var item in await _menuService.Menus())
                        {
                            //add menues that wasnt added before
                            if (Request.Form["Page[" + item.MenuId.ToString() + "]"].Any() && !await _modulePageService.ExistModulePage(menuModulViewModel.ModuleId, item.MenuId))
                            {
                                ModulePage modulePage = new ModulePage()
                                {
                                    MenuId   = item.MenuId,
                                    ModuleId = menuModulViewModel.ModuleId,
                                };
                                modulePageAddList.Add(modulePage);
                            }
                            //remove menues that was added before
                            else if (!Request.Form["Page[" + item.MenuId.ToString() + "]"].Any() && await _modulePageService.ExistModulePage(menuModulViewModel.ModuleId, item.MenuId))
                            {
                                ModulePage PageRemove = await _modulePageService.GetByMenuModule(menuModulViewModel.ModuleId, item.MenuId);

                                modulePageRemoveList.Add(PageRemove);
                            }
                        }
                        await _modulePageService.Add(modulePageAddList);

                        await _modulePageService.Remove(modulePageRemoveList);

                        //editing MenuModule GroupId if it's changed
                        var EditMenuModule = await _menuModuleService.GetByModuleId(menuModulViewModel.ModuleId);

                        if (EditMenuModule.MenuGroupId != menuModulViewModel.MenuGroupId)
                        {
                            EditMenuModule.MenuGroupId = menuModulViewModel.MenuGroupId;
                            await _menuModuleService.Edit(EditMenuModule);
                        }

                        //Editing the Module
                        await _moduleService.Edit(module, pastPosition, pastDisOrder);
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MenuGroupId"] = new SelectList(await _menuGroupService.MenuGroup(), "MenuGroupId", "MenuTitile");
            ViewData["PositionId"]  = new SelectList(await _positionService.GetAll(), "PositionId", "PositionTitle");
            return(View(menuModulViewModel));
        }