public ActionResult ShelterPost() { var prodId = 0; try { // Get product name var product = Request.Form["Product[0]"]; //Create a list to hold list of items var prodList = new List <Shelterdescription>(); var productExist = BaseRepository.OimsDataContext.Shelters.Any(n => n.P_Name == product); if (!productExist) { // Create product, save and get product id var prod = new Shelter { P_Name = product }; prodId = ShelterRepository.SaveShelter(prod); if (prodId > 0) { //Loop through the request.forms for (var i = 0; i <= Request.Form.Count; i++) { var item = Request.Form["Item[" + i + "]"]; var qty = Request.Form["Quantity[" + i + "]"]; if (item == null || qty == null) { continue; } // Create product items list prepared var prodDesc = new Shelterdescription { P_Id = prodId, I_Id = Convert.ToInt32(item), I_Qty = Convert.ToInt32(qty) }; prodList.Add(prodDesc); } } } else { throw new CustomException(Supervisor.SErrorMsgProductExist); } // Save items of product to database if (prodList.Count > 0) { var duplicateExist = prodList.GroupBy(n => n.I_Id).Any(c => c.Count() > 1); if (duplicateExist) { DeleteShelter(prodId); TempData["ErrorMessage"] = string.Format(Common.ErrorDiv, Supervisor.SErrorMsgSameItem); } else { ShelterRepository.SaveShelterItems(prodList); TempData["ConfirmMessage"] = string.Format(Common.ConfirmDiv, string.Format(Supervisor.SMsgProductCreated, product)); } } else { DeleteShelter(prodId); TempData["ErrorMessage"] = string.Format(Common.ErrorDiv, Common.ErrorMsgBlankFields); } } catch (Exception exc) { DeleteShelter(prodId); TempData["ErrorMessage"] = string.Format(Common.ErrorDiv, exc.Message); Logger.LogError(exc, "Error while saving shelter from Supervisor zone"); } return(PartialView("_Result")); }