public async Task <IActionResult> Edit(int id, [Bind("BrokerId,FirstName,LastName,Email,Phonenumber,Image,ImageText,HomeId, Title")] Broker broker, BrokersViewModel model) { if (id != broker.BrokerId) { return(NotFound()); } if (ModelState.IsValid) { try { //en bild string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); /* * Broker img = new Broker(); * img.ImageText = uniqueFileName; * img.Image = uniqueFileName; * * * _context.Update(img); */ broker.Image = uniqueFileName; } _context.Update(broker); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BrokerExists(broker.BrokerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(broker)); }
public async Task <IActionResult> Create([Bind("BrokerId,FirstName,LastName,Email,Phonenumber,Image,ImageText,HomeId, Title")] Broker broker, bool HeroImage2, BrokersViewModel model) { if (ModelState.IsValid) { //en bild string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); broker.Image = uniqueFileName; } _context.Add(broker); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(broker)); }