public ActionResult GetImage(int id) { CupImage c = db.CupImages.FirstOrDefault(d => d.CupImageID == id); if (c != null && c.Image != null) { return(File(c.Image, "image/png")); } return(View()); }
// [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] public async Task <IActionResult> GetImage(int id) { Response.Headers.Add("Cache-Control", "no-store"); CupImage c = await db.CupImages.SingleOrDefaultAsync(m => m.CupImageID == id); if (c != null && c.Image != null) { return(File(c.Image, "image/png")); } return(GetDefaultImage()); }
public ActionResult AddOrEdit([Bind(Include = "CupId,Capacity,CupType,CountryID,ImagePath,ImageUpload")] Cup cup) { try { if (ModelState.IsValid) { ///var c = db.Cups.Where(x => x.CupId == cup.CupId).FirstOrDefault<Cup>(); if (cup.CupId == 0) { db.Cups.Add(cup); db.SaveChanges(); if (cup.ImageUpload != null) { CupImage cupImg = new CupImage(); cupImg.CupImageID = cup.CupId; cupImg.Image = new byte[cup.ImageUpload.ContentLength]; cup.ImageUpload.InputStream.Read(cupImg.Image, 0, cup.ImageUpload.ContentLength); cupImg.Cup = cup; db.CupImages.Add(cupImg); db.SaveChanges(); } } else { if (cup.ImageUpload != null) { var cupImg = db.CupImages.Find(cup.CupId); if (cupImg != null) { cupImg.Image = new byte[cup.ImageUpload.ContentLength]; cup.ImageUpload.InputStream.Read(cupImg.Image, 0, cup.ImageUpload.ContentLength); db.Entry(cupImg).State = EntityState.Modified; db.SaveChanges(); } } db.Entry(cup).State = EntityState.Modified; db.SaveChanges(); } } ViewBag.CountryID = new SelectList(db.Countries, "CountryId", "CountryName", cup.CountryID); return(RedirectToAction("Index")); //return Json(new { success = true, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", GetAllCups()), message = "Operation was Successfully" }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return(Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet)); } }
public async Task <IActionResult> AddOrEditCup(Cup cup) { try { if (ModelState.IsValid) { if (cup.CupId == 0) { db.Cups.Add(cup); db.SaveChanges(); if (cup.ImageUpload != null) { var cupImg = new CupImage(); cupImg.CupImageID = cup.CupId; cupImg.Image = new byte[cup.ImageUpload.Length]; using (var ms = new MemoryStream()) { cup.ImageUpload.CopyTo(ms); cupImg.Image = ms.ToArray(); } cupImg.Cup = cup; db.CupImages.Add(cupImg); await db.SaveChangesAsync(); } } else { if (cup.ImageUpload != null) { var img = new byte[cup.ImageUpload.Length]; using (var ms = new MemoryStream()) { cup.ImageUpload.CopyTo(ms); img = ms.ToArray(); } var cupImg = await db.CupImages.SingleOrDefaultAsync(m => m.CupImageID == cup.CupId); if (cupImg == null) { cupImg = new CupImage { CupImageID = cup.CupId, Image = img }; db.CupImages.Add(cupImg); await db.SaveChangesAsync(); } else { cupImg.Image = img; db.Update(cupImg); await db.SaveChangesAsync(); } } db.Update(cup); await db.SaveChangesAsync(); } ViewBag.CountryID = new SelectList(db.Countries, "CountryId", "CountryName", cup.CountryID); var result = await _viewRenderService.RenderToStringAsync("Cups/ViewAll", GetAllCupsAsync()); return(Json(new { success = true, result, message = "Operation was Successfully" })); } else { var result = await _viewRenderService.RenderToStringAsync("Cups/ViewAll", GetAllCupsAsync()); return(Json(new { success = false, result, message = "Can't add, because input values are invalid" })); } } catch (Exception ex) { return(Json(new { success = false, message = ex.Message })); } }