public Operation Save(SlsRoute record, IList<SlsRouteDetail> recordDetails)
        {
            Operation objOperation = new Operation { Success = false };
            using (var dbContextTransaction = _repository.BeginTransaction())
            {
                try
                {
                    objOperation = new Operation { Success = true };

                    int Id = _repository.AddEntity(record);
                    _repository.SaveChanges();
                    objOperation.OperationId = Id;
                    record.Id = Id;

                    IList<SlsRouteDetail> newrecordDetails = new List<SlsRouteDetail>();
                    //add or update categories offered to each offer
                    if (recordDetails != null && recordDetails.Count > 0)
                    {
                        foreach (SlsRouteDetail detail in recordDetails)
                        {
                            detail.SlsRouteId = record.Id;
                            if (detail.Id <= 0)
                                newrecordDetails.Add(detail);

                                else
                                _detailrepository.Update(detail);

                        }
                    }
                    //Add offer detail list - new offer details
                    if (newrecordDetails != null && newrecordDetails.Count > 0)
                    {
                        _detailrepository.AddEntityList(newrecordDetails);
                    }
                    _detailrepository.SaveChanges();

                    try
                    {
                        //_unitOfWork.Commit();
                        _repository.Commit(dbContextTransaction);
                    }
                    catch (Exception ex)
                    {
                        objOperation.Success = false;
                        throw ex;
                    }
                }
                catch (Exception ex)
                {
                    _repository.Rollback(dbContextTransaction);
                }
            }
            return objOperation;
        }
        public ActionResult Save(SlsRoute record, IList<SlsRouteDetail> detailRecords)
        {
            int userId = Convert.ToInt32(Session["userId"]);
            Operation objOperation = new Operation { Success = false };

            if (ModelState.IsValid && record != null && detailRecords != null && detailRecords.Count > 0)
            {
                foreach (SlsRouteDetail item in detailRecords)
                {
                    if (item.SlsDistributorId == 0)
                        item.SlsDistributorId = null;
                    if (item.SlsRetailerId == 0)
                        item.SlsRetailerId = null;
                    if (item.SlsDealerId == 0)
                        item.SlsDealerId = null;
                    if (item.SlsCorporateClientId == 0)
                        item.SlsCorporateClientId = null;
                }

                if (record.Id == 0)
                {
                    if ((bool)Session["Add"])
                    {

                        record.CreatedBy = userId;
                        record.CreatedDate = DateTime.Now.Date;
                        objOperation = _service.Save(record, detailRecords);
                    }
                    else { objOperation.OperationId = -1; }

                }
                else
                {
                    if ((bool)Session["Edit"])
                    {
                        record.ModifiedBy = userId;
                        record.ModifiedDate = DateTime.Now.Date;
                        objOperation = _service.Update(record, detailRecords);
                    }
                    else { objOperation.OperationId = -2; }
                }
            }

            return Json(objOperation, JsonRequestBehavior.AllowGet);
            //return null;
        }
        public Operation Update(SlsRoute record, IList<SlsRouteDetail> recordDetails)
        {
            Operation objOperation = new Operation { Success = true, OperationId = record.Id };
            using (var dbContextTransaction = _repository.BeginTransaction())
            {
                try
                {
                    objOperation = new Operation { Success = true };

                    _repository.Update(record);
                    _repository.SaveChanges();

                    IList<SlsRouteDetail> newrecordDetails = new List<SlsRouteDetail>();
                    //add or update categories offered to each offer
                    if (recordDetails != null && recordDetails.Count > 0)
                    {
                        foreach (SlsRouteDetail detail in recordDetails)
                        {
                            detail.SlsRouteId = record.Id;
                            if (detail.Id <= 0)
                                newrecordDetails.Add(detail);

                            else
                                _detailrepository.Update(detail);

                        }
                    }
                    //Add offer detail list - new offer details
                    if (newrecordDetails != null && newrecordDetails.Count > 0)
                    {
                        _detailrepository.AddEntityList(newrecordDetails);
                    }
                    IList<SlsRouteDetail> fbrecordDetails = GetRouteSetupDetails(record.Id);
                    IList<int> updatedDetailIds = recordDetails.Where(i => i.Id > 0).Select(i => i.Id).ToList();
                    IList<SlsRouteDetail> removedrecordDetails = fbrecordDetails.Where(i => !updatedDetailIds.Contains(i.Id)).ToList();
                    if (removedrecordDetails != null && removedrecordDetails.Count > 0)
                    {
                        foreach (SlsRouteDetail detail in removedrecordDetails)
                        {
                            _detailrepository.Delete(detail);
                        }
                    }
                    _detailrepository.SaveChanges();

                    try
                    {
                        //_unitOfWork.Commit();
                        _repository.Commit(dbContextTransaction);
                    }
                    catch (Exception ex)
                    {
                        objOperation.Success = false;
                        throw ex;
                    }
                }
                catch (Exception ex)
                {
                    _repository.Rollback(dbContextTransaction);
                }
            }
            return objOperation;
        }