public JsonResult AddAndEdit(SubMenuActionModel model) { JsonResult json = new JsonResult(); var result = false; List <int> pictureIDs = !string.IsNullOrEmpty(model.PictureIDs) ? model.PictureIDs.Split(',').Select(x => int.Parse(x)).ToList() : new List <int>(); var pictures = sharedServices.GetPictureByIDs(pictureIDs); if (ModelState.IsValid) { if (model.SubMenuID > 0) { var subMenu = subServices.GetSubMenuByID(model.SubMenuID); subMenu.MainMenuID = model.MainMenuID; subMenu.SubMenuName = model.SubMenuName; subMenu.Description = model.Description; subMenu.UpdatedAt = DateTime.Now; subMenu.SubMenuPictures.Clear(); subMenu.SubMenuPictures.AddRange(pictures.Select(x => new SubMenuPicture() { SubMenuID = subMenu.SubMenuID, PictureID = x.PictureID })); result = subServices.UpdateSubMenu(subMenu); } else { SubMenu subMenu = new SubMenu(); subMenu.MainMenuID = model.MainMenuID; subMenu.SubMenuName = model.SubMenuName; subMenu.Description = model.Description; subMenu.UpdatedAt = DateTime.Now; subMenu.SubMenuPictures = new List <SubMenuPicture>(); subMenu.SubMenuPictures.AddRange(pictures.Select(x => new SubMenuPicture() { PictureID = x.PictureID })); result = subServices.SaveSubMenu(subMenu); } } if (result) { json.Data = new { Success = true }; } else { json.Data = new { Success = false, Message = "Unable to perform any action" }; } return(json); }