//This method is to utilize to get Intial model for all Fruits available in Create operation public CheckBoxFruits GetFruitsInitialModel() { var model = new CheckBoxFruits(); var selectedFruits = new List <Fruit>(); model.AllFruits = GetAllFruits(); model.SelectedFruits = selectedFruits; //model.PostedNewlyUpdatedFruitIDs = new int[0]; model.PostedNewlyUpdatedFruitIDs = new int[0]; return(model); }
//This method is to utilize for Edit operation GET method to fetch selected values to form - CHECKBOX private CheckBoxFruits GetAlreadySelectedFruitsModel(int[] PostedUpdatedFruitIDs) { var model = new CheckBoxFruits(); var NewlyUpdatedFruits = new List <Fruit>(); if (PostedUpdatedFruitIDs == null) { PostedUpdatedFruitIDs = new int[0]; } else if (PostedUpdatedFruitIDs != null && PostedUpdatedFruitIDs.Any()) // if array of NewlyUpdatedFruitIDs exists and is not empty,save selected ids and create a list of fruits with that { NewlyUpdatedFruits = GetAllFruits() .Where(x => PostedUpdatedFruitIDs.Any(s => x.Id.ToString().Equals(s))) .ToList(); } model.AllFruits = GetAllFruits(); model.SelectedFruits = NewlyUpdatedFruits; model.PostedNewlyUpdatedFruitIDs = PostedUpdatedFruitIDs; return(model); }