public ActionResult Create([Bind(Include = "ProductListingID,AddedDate,ProductName,Source")] Product product, string[] selectedComplianceForms, HttpPostedFileBase upload) { if (selectedComplianceForms != null) { product.ComplianceForms = new List<ComplianceForm>(); foreach (var complianceforms in selectedComplianceForms) { var complianceformToAdd = db.ComplianceForms.Find(int.Parse(complianceforms)); product.ComplianceForms.Add(complianceformToAdd); } } if (ModelState.IsValid) { if (upload != null && upload.ContentLength > 0) { var avatar = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.Avatar, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { avatar.Content = reader.ReadBytes(upload.ContentLength); } product.Files = new List<File> { avatar }; } db.Products.Add(product); db.SaveChanges(); return RedirectToAction("Index"); } PopulateAssignedComplianceFormData(product); return View(product); }
public ActionResult Edit(int? id, string[] selectedComplinanceform, HttpPostedFileBase upload) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var ProductToUpdate = db.Products .Include(i => i.ComplianceForms) .Where(i => i.ProductListingID == id) .Single(); if (TryUpdateModel(ProductToUpdate, "", new string[] { "ProductListingID,AddedDate,ProductName,Source" })) { try { //if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location)) // { // instructorToUpdate.OfficeAssignment = null; // } if (upload != null && upload.ContentLength > 0) { if (ProductToUpdate.Files.Any(f => f.FileType == FileType.Avatar)) { db.Files.Remove(ProductToUpdate.Files.First(f => f.FileType == FileType.Avatar)); } var avatar = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.Avatar, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { avatar.Content = reader.ReadBytes(upload.ContentLength); } ProductToUpdate.Files = new List<File> { avatar }; } UpdateProduct(selectedComplinanceform, ProductToUpdate); db.SaveChanges(); 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."); } } PopulateAssignedComplianceFormData(ProductToUpdate); return View(ProductToUpdate); }