コード例 #1
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);
        }