public ActionResult Create(TourDayModel model) { if (ModelState.IsValid) { try { if (Request.Files["TourDayImage"].ContentLength > 0) { HttpPostedFileBase imageFile = Request.Files["TourDayImage"]; model.TourDayImage = imageFile.FileName; //Get the physical path for the uploads folder and make sure it exists string folderToUpload = Server.MapPath("~/images/tours/"); //Resize Images Generate each version ImageUtils.TourDayImageResize(imageFile, folderToUpload); } model.Description = HttpUtility.HtmlEncode(model.Description); model.CreatedBy = GetUser().UserId; model.CreatedOn = DateTime.UtcNow; TourDayModel addedTourDay = TourDayRepository.CreateTourDay(model.ToEntity()); if (addedTourDay != null) // return RedirectToAction("Tours", new { message = MessageType.Success }); return RedirectToAction("index", new { message = MessageType.Success, id = addedTourDay.TourId }); return View("Create", new { message = MessageType.Error }); } catch (TourDayExistsException e) { //ModelState.AddModelError("", e.Message); ViewBag.MessageType = MessageType.Error; ViewBag.Message = e.Message; return View(model); } catch (Exception) { throw; } } return View(model); }
/// <summary> /// Create a new tour day for given tour /// </summary> /// <param name="message"></param> /// <param name="id">Tour Id</param> /// <returns></returns> public ActionResult Create(MessageType? message, int id) { ViewBag.MessageType = message; ViewBag.Message = message == MessageType.Success ? "Your request complete successfully!" : message == MessageType.Error ? "Unable to complete your request; an error occurred!" : message == MessageType.Info ? "" : null; try { TourModel tour = TourRepository.GetTour(id); TourDayModel model = new TourDayModel(tour); return View(model); } catch (Exception) { throw; } }
internal static TourDayModel UpdateTourDay(TourDayModel model) { try { using (SriLankaToursEntities context = new SriLankaToursEntities()) { TourDayEntity entity = context.TourDayEntities.Find(model.TourId, model.Day); if (entity == null) throw new TourDayNotFoundException(); bool foundDuplicates = (from d in context.TourDayEntities where d.TourId == model.TourId && d.Day == model.Day && d.TourDayId != model.TourDayId select d).Any(); if(foundDuplicates) throw new TourDayExistsException("Duplicate record found with same day and tour"); if (string.IsNullOrEmpty(model.TourDayImage)) model.TourDayImage = entity.TourDayImage; entity.Title = model.Title; entity.TourDayImage = model.TourDayImage; entity.Description = model.Description; context.SaveChanges(); return GetTourDay(entity.TourDayId); } } catch (Exception) { throw; } }
public ActionResult Edit(TourDayModel model) { try { ModelState state = ModelState["TourDayImage"]; if (state != null) state.Errors.Clear(); if (ModelState.IsValid) { model.UpdatedOn = DateTime.UtcNow; model.UpdatedBy = GetUser().UserId; if (Request.Files["TourDayImage"].ContentLength > 0) { HttpPostedFileBase imageFile = Request.Files["TourBannerImage"]; model.TourDayImage = imageFile.FileName; //Get the physical path for the uploads folder and make sure it exists string folderToUpload = Server.MapPath("~/images/tours/"); //Resize Images Generate each version ImageUtils.TourImageResize(imageFile, folderToUpload); } TourDayModel tourDayUpdated = TourDayRepository.UpdateTourDay(model); if (tourDayUpdated != null) return RedirectToAction("index", new { message = MessageType.Success, id = tourDayUpdated.TourId }); throw new Exception(); } return View(model); } catch (Exception) { throw; } }
public ActionResult Delete(TourDayModel tourDay) { try { bool status = TourDayRepository.DeleteTourDay(tourDay.TourDayId); if (!status) return RedirectToAction("index", new { message = MessageType.Error, id=tourDay.TourId }); return RedirectToAction("index", new { message = MessageType.Success, id=tourDay.TourId }); } catch (TourDayNotFoundException) { throw; } catch (Exception) { throw; } }