public async Task <IActionResult> Create([Bind("Title,Upload,Photo,Id")] TestimonialItem testimonial) { if (testimonial.Upload == null) { ModelState.AddModelError("Upload", "The Photo field is required."); } else { if (testimonial.Upload.ContentType != "image/jpeg" && testimonial.Upload.ContentType != "image/png" && testimonial.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "You can only download png, jpg or gif file"); } if (testimonial.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB"); } } if (ModelState.IsValid) { var fileName = _fileManager.Upload(testimonial.Upload); testimonial.Photo = fileName; _context.Add(testimonial); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(testimonial)); }
public async Task <IActionResult> Edit(int id, [Bind("Title,Upload,Photo,Id")] TestimonialItem testimonial) { if (id != testimonial.Id) { return(NotFound()); } if (testimonial.Upload == null) { ModelState.AddModelError("Upload", "The Photo field is required."); } if (ModelState.IsValid) { try { if (testimonial.Upload != null) { if (testimonial.Upload.ContentType != "image/jpeg" && testimonial.Upload.ContentType != "image/png" && testimonial.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "You can only download png, jpg or gif file"); return(View(testimonial)); } if (testimonial.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB"); return(View(testimonial)); } var oldFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", testimonial.Photo); _fileManager.Delete(oldFile); var fileName = _fileManager.Upload(testimonial.Upload, "wwwroot/uploads"); testimonial.Photo = fileName; } _context.Update(testimonial); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TestimonialExsist(testimonial.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(testimonial)); }
private ItemViewModel GetItemViewModel(TestimonialItem item) { var viewModel = new ItemViewModel() { Title = item.TestimonialAuthor, Quote = item.Quote, Company = item.Company }; if (item.Photo != null && item.Photo.Length == 1) { viewModel.ThumbnailUrl = item.Photo[0].ThumbnailUrl; } return(viewModel); }
public async Task <IActionResult> Index(string lang) { lang = String.IsNullOrWhiteSpace(lang) ? "de" : lang; var model = new AboutMeViewModel(); Utils.CheckOrRefreshUiStrings(lang); ViewBag.Title = Utils.GetUiString("title.aboutMe"); ViewBag.ActiveLink = 0; using (var db = new MysqlDbContext(this.ConnectionString)) { // CV var cvEntries = await db.Cv.ToListAsync(); cvEntries = cvEntries.OrderByDescending(c => c.StartDate).ToList(); var cvItems = new List <CvItem>(); // Testimonials var testimonials = await db.Testimonials.ToListAsync(); var testimonialItems = new List <TestimonialItem>(); foreach (var cv in cvEntries) { var cvItem = new CvItem(); cvItem.Entry = cv; var attachment = await db.Media.FirstOrDefaultAsync(m => m.Id == cv.Attachment); var thumbnail = await db.Media.FirstOrDefaultAsync(m => m.Id == cv.Thumbnail); if (attachment != null) { cvItem.Attachment = attachment; } if (thumbnail != null) { cvItem.Thumbnail = thumbnail; } cvItems.Add(cvItem); } foreach (var t in testimonials) { var tItem = new TestimonialItem(); tItem.Testimonial = t; var image = await db.Media.FirstOrDefaultAsync(i => i.Id == t.Image); if (image != null) { tItem.Image = image; } testimonialItems.Add(tItem); } model.CvEntries = cvItems; model.Testimonials = testimonialItems; } return(this.View(model)); }