コード例 #1
0
        public ActionResult AddTour(Int32 id = -1)
        {
            if (id != -1)
            {
                var tourRepository = new TourRepository();
                Tour tour = tourRepository.GetById(id);
                if (tour != null)
                {
                    var model = new TourModel
                    {
                        Id = tour.Id,
                        Name = tour.Name,
                        StartDate = tour.StartDate,
                        EndDate = tour.EndDate,
                        PlaceCount = tour.PlaceCount,
                        Price = tour.Price,
                        Description = tour.Description
                    };

                    return View(model);
                }
            }

            return View(new TourModel());
        }
コード例 #2
0
        public ActionResult AddTour(TourModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    model.ImageMimeType = image.ContentType;
                    model.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(model.ImageData, 0, image.ContentLength);
                }

                Tour tour = new Tour
                {
                    Name = model.Name,
                    StartDate = model.StartDate.Value,
                    EndDate = model.EndDate.Value,
                    PlaceCount = model.PlaceCount,
                    Price = model.Price,
                    Description = model.Description,
                    Image = model.ImageData
                };

                var tourRepository = new TourRepository();

                if (model.Id.HasValue)
                {
                    tour.Id = model.Id.Value;
                    tourRepository.UpdateTour(tour);
                }
                else
                {
                    tourRepository.AddNewTour(tour);
                }

                return RedirectToAction("Index", "Home");
            }
            return View(model);
        }