コード例 #1
0
        public async Task <IActionResult> AddEvent(EventVM model)
        {
            if (ModelState.IsValid)
            {
                var    restaurant     = _RepositoryEvent.GetRestaurant(model.RestaurantId).Result;
                string uniqueFileName = null;


                if (model.File != null)
                {
                    string uploadsFolder = Path.Combine(_env.WebRootPath, "ImageEvent");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.File.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    await model.File.CopyToAsync(new FileStream(filePath, FileMode.Create));
                }

                EventRestaurant newEvent = new EventRestaurant
                {
                    EventName  = model.Name,
                    DateStart  = model.DateStart,
                    DateEnd    = model.DateEnd,
                    PhotoUrl   = uniqueFileName,
                    Place      = model.Place,
                    Restaurant = restaurant
                };
                _RepositoryEvent.AddEventRestaurant(newEvent);

                return(RedirectToAction(nameof(Events)));
            }

            return(View());
        }