Exemple #1
0
        public ActionResult SaveAjax(ServiceProvider serviceprovider)
        {
            //id=0 means add operation, update operation otherwise
            bool isNew = serviceprovider.ID == 0;

            if (serviceprovider.RegionID == null || serviceprovider.RegionID == 0)
            {
                serviceprovider.ErrorMessage = "Region is required";
            }
            //validate data
            if (ModelState.IsValid)
            {
                try
                {
                    //var data = regionRepository.FindAllByWorkerID(CurrentLoggedInWorker.ID, 0).Where(item => item.IsActive == true).Select(m=>m.ID).ToList();
                    //if (!data.Contains(Convert.ToInt32(serviceprovider.RegionID)))
                    //{
                    //    WebHelper.CurrentSession.Content.ErrorMessage = "You are not eligible to do this action";
                    //    return RedirectToAction(Constants.Actions.AccessDenied, Constants.Controllers.Home, new { Area = String.Empty });
                    //}
                    //call repository function to save the data in database
                    serviceproviderRepository.InsertOrUpdate(serviceprovider);
                    serviceproviderRepository.Save();
                    //set status message
                    if (isNew)
                    {
                        serviceprovider.SuccessMessage = "ServiceProvider has been added successfully";
                    }
                    else
                    {
                        serviceprovider.SuccessMessage = "ServiceProvider has been updated successfully";
                    }
                }
                catch (CustomException ex)
                {
                    serviceprovider.ErrorMessage = ex.UserDefinedMessage;
                }
                catch (Exception ex)
                {
                    ExceptionManager.Manage(ex);
                    serviceprovider.ErrorMessage = Constants.Messages.UnhandelledError;
                }
            }
            else
            {
                foreach (var modelStateValue in ViewData.ModelState.Values)
                {
                    foreach (var error in modelStateValue.Errors)
                    {
                        serviceprovider.ErrorMessage = error.ErrorMessage;
                        break;
                    }
                    if (serviceprovider.ErrorMessage.IsNotNullOrEmpty())
                    {
                        break;
                    }
                }
            }
            //return the status message in json
            if (serviceprovider.ErrorMessage.IsNotNullOrEmpty())
            {
                return(Json(new { success = false, data = serviceprovider.ErrorMessage }));
            }
            else
            {
                return(Json(new { success = true, data = serviceprovider.SuccessMessage }));
            }
        }