public async Task<ActionResult> Edit(string id,[Bind(Include = "ReptileId,Gender,SpeciesName,ScientificName,CommonName,Born,Morph,Venomous,Weight,WeightProgress,Origin,Food,FeedingType,AdultSize,Habitat,Breeder,BreederEmail,Cities,ClutchID,ForSale,Price,NickName,LicenseNumber,ChipNumber,SpeciesNumber,FatherNotInDb,MotherNotInDb,FeedInterval,DueDate,TubeBoxNumber,Note,SalesCardComment")] Reptile reptile,HttpPostedFileBase upload) { if(reptile.ReptileId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var reptileToUpdate = await db.Reptiles.FindAsync(id); if(TryUpdateModel(reptileToUpdate,"", new string[] {"ReptileId,Gender,SpeciesName,ScientificName,CommonName,Born,Morph,Venomous,Weight,WeightProgress,Origin,Food,FeedingType,AdultSize,Habitat,Breeder,BreederEmail,Cities,ClutchID,ForSale,Price,NickName,LicenseNumber,ChipNumber,SpeciesNumber,FatherNotInDb,MotherNotInDb,FeedInterval,DueDate,TubeBoxNumber,Note,SalesCardComment"})) { try { if(upload != null && upload.ContentLength > 0) { if (reptileToUpdate.Files.Any(f => f.FileType == FileType.image)) { db.Files.Remove(reptileToUpdate.Files.First(f => f.FileType == FileType.image)); } var image = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.image, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { image.Content = reader.ReadBytes(upload.ContentLength); } reptileToUpdate.Files = new List<File> { image }; } db.Entry(reptileToUpdate).State = EntityState.Modified; await db.SaveChangesAsync(); return RedirectToAction("Index"); } 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 View(reptileToUpdate); }
// [Authorize(Roles = "canEdit")] public async Task<ActionResult> Create([Bind(Include = "ReptileId,Gender,SpeciesName,ScientificName,CommonName,Born,Morph,Venomous,Weight,WeightProgress,Origin,Food,FeedingType,AdultSize,Habitat,Breeder,BreederEmail,Cities,ClutchID,ForSale,Price,NickName,LicenseNumber,ChipNumber,SpeciesNumber,FatherNotInDb,MotherNotInDb,FeedInterval,TimeStamp,DueDate,TubeBoxNumber,Note,SalesCardComment")] Reptile reptile, HttpPostedFileBase upload) { try { if (ModelState.IsValid) { if (upload != null && upload.ContentLength > 0) { var image = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.image, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { image.Content = reader.ReadBytes(upload.ContentLength); } reptile.Files = new List<File> { image }; } //creates qr code and saves it as binary data var newQr = reptile.QrGen(); var byteContent = reptile.imageToByteArray(newQr); reptile.QRCode = byteContent; reptile.TimeStamp = DateTime.Today.ToLocalTime(); db.Reptiles.Add(reptile); await db.SaveChangesAsync(); return RedirectToAction("Index"); } } catch (RetryLimitExceededException /*dex*/ ) { //Log the error (uncomment dex variable name and add a line here to write a log. //dex.GetBaseException(); ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(reptile); }