Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("CottageId,Name,Price,NumberOfGuest,AnimalsAllowed,Description,IsBooked,Photo")] CottageCreateViewModel cottageModel)
        {
            if (id != cottageModel.CottageId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Tilldelar värden för uppdatering av stuga
                    var cottage = await _context.Cottage
                                  .FirstOrDefaultAsync(m => m.CottageId == id);

                    cottage.Name           = cottageModel.Name;
                    cottage.Price          = cottageModel.Price;
                    cottage.NumberOfGuest  = cottageModel.NumberOfGuest;
                    cottage.AnimalsAllowed = cottageModel.AnimalsAllowed;
                    cottage.Description    = cottageModel.Description;
                    cottage.IsBooked       = cottageModel.IsBooked;
                    cottage.PhotoPath      = cottage.PhotoPath;


                    _context.Update(cottage);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CottageExists(cottageModel.CottageId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("CottageId,Name,Price,NumberOfGuest,AnimalsAllowed,Description,IsBooked,Photo")] CottageCreateViewModel cottageModel)
        {
            if (ModelState.IsValid)
            {
                //Kod för bilduppladdning
                string cottageFileName = null;
                if (cottageModel.Photo != null)
                {
                    //Mapp där bilderna lagras
                    string imagesFolder = Path.Combine(hostingEnvironment.WebRootPath, "img/cottageimg");

                    //Filnamn
                    cottageFileName = Guid.NewGuid().ToString() + "_" + cottageModel.Photo.FileName;

                    //Sökväg till bild
                    string filePath = Path.Combine(imagesFolder, cottageFileName);

                    //Kopierar över till server
                    cottageModel.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                //Lägger till alla värden
                Cottage newCottage = new Cottage
                {
                    Name           = cottageModel.Name,
                    Price          = cottageModel.Price,
                    NumberOfGuest  = cottageModel.NumberOfGuest,
                    AnimalsAllowed = cottageModel.AnimalsAllowed,
                    Description    = cottageModel.Description,
                    IsBooked       = cottageModel.IsBooked,
                    PhotoPath      = cottageFileName
                };

                _context.Add(newCottage);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }