public ActionResult AddPictures(BillPicture billPicture, HttpPostedFileBase upload) { try { var dbBill = db.Bills.Single(s => s.Id == billPicture.Bill_Id); var userId = User.Identity.GetUserId(); var billPic = new BillPicture { CreationDate = DateTime.UtcNow, CreateUser = db.Users.Single(u => u.Id == userId), Name = System.IO.Path.GetFileName(upload.FileName), LinkedFile = new File { FileType = FileType.ShopPicture, ContentType = upload.ContentType, }, Bill_Id = dbBill.Id }; // Parametre 'LeaveOpen = true' car la validation a peut etre besoin de relire la stream (sinon le Dispose() du BinaryReader efface le contenu) using (var reader = new System.IO.BinaryReader(upload.InputStream, Encoding.Default, true)) { billPic.LinkedFile.Content = reader.ReadBytes(upload.ContentLength); } var spv = new PictureValidation <BillPicture>(); spv.Validate(billPicture, ModelState); spv.Validate(upload, ModelState); if (!ModelState.IsValid) { return(View(billPicture)); } db.BillPictures.Add(billPic); db.SaveChanges(); } catch (DbUpdateException e) { ModelState.AddModelError("", e.Message); } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(RedirectToAction("Edit", new { Id = billPicture.Bill_Id })); }
public ActionResult AddPictures(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var bill = db.Bills.Find(id); if (bill == null) { return(HttpNotFound()); } var billPicture = new BillPicture { Bill_Id = id.Value, Bill = bill }; return(View(billPicture)); }