public ActionResult Create([Bind(Include = "ProjectID,Title,TEC,DivisionID,BuildingID,SMEID,Description,Justification,ActivityTypeID,FundingTypeID,Cost,NeedTypeID,BandRTypeID,IFISectionTypeID,DM,StatusTypeID,CampusStrategyID,LOBCategoryID,LOBRating,ShovelReady,EnrollmentDate,FY")] Project project, HttpPostedFileBase upload) { if (ModelState.IsValid) { if (upload != null && upload.ContentLength > 0) { var image = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileGUID = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), FileType = FileType.Image, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { image.Content = reader.ReadBytes(upload.ContentLength); } project.Files = new List<File> { image }; } db.Projects.Add(project); db.SaveChanges(); return RedirectToAction("Details", new { id = project.ProjectID }); } ViewBag.ActivityTypeID = new SelectList(db.ActivityTypes, "ActivityTypeID", "Title", project.ActivityTypeID); ViewBag.BandRTypeID = new SelectList(db.BandRTypes, "BandRTypeID", "Title", project.BandRTypeID); ViewBag.BuildingID = new SelectList(db.Buildings, "BuildingID", "Name", project.BuildingID); ViewBag.CampusStrategyID = new SelectList(db.CampusStrategies, "CampusStrategyID", "Title", project.CampusStrategyID); ViewBag.DivisionID = new SelectList(db.Divisions, "DivisionID", "Title", project.DivisionID); ViewBag.FundingTypeID = new SelectList(db.FundingTypes, "FundingTypeID", "Title", project.FundingTypeID); ViewBag.IFISectionTypeID = new SelectList(db.IFISectionTypes, "IFISectionTypeID", "Title", project.IFISectionTypeID); ViewBag.LOBCategoryID = new SelectList(db.LOBCategories, "LOBCategoryID", "Title", project.LOBCategoryID); ViewBag.NeedTypeID = new SelectList(db.NeedTypes, "NeedTypeID", "Title", project.NeedTypeID); ViewBag.StatusTypeID = new SelectList(db.StatusTypes, "StatusTypeID", "Title", project.StatusTypeID); return View(project); }
public ActionResult EditPost(int? id, HttpPostedFileBase upload) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var projectToUpdate = db.Projects.Find(id); if (TryUpdateModel(projectToUpdate, "")) { try { if (upload != null && upload.ContentLength > 0) { //Uncomment below if only on file should exist //if (projectToUpdate.Files.Any(f => f.FileType == FileType.Image)) //{ // db.Files.Remove(projectToUpdate.Files.First(f => f.FileType == FileType.Image)); //} var image = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileGUID = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), FileType = FileType.Image, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { image.Content = reader.ReadBytes(upload.ContentLength); } projectToUpdate.Files = new List<File> { image }; } db.Entry(projectToUpdate).State = EntityState.Modified; 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."); } } /* ViewBag.ActivityTypeID = new SelectList(db.ActivityTypes, "ActivityTypeID", "Title", project.ActivityTypeID); ViewBag.BandRTypeID = new SelectList(db.BandRTypes, "BandRTypeID", "Title", project.BandRTypeID); ViewBag.BuildingID = new SelectList(db.Buildings, "BuildingID", "Name", project.BuildingID); ViewBag.CampusStrategyID = new SelectList(db.CampusStrategies, "CampusStrategyID", "Title", project.CampusStrategyID); ViewBag.DivisionID = new SelectList(db.Divisions, "DivisionID", "Title", project.DivisionID); ViewBag.FundingTypeID = new SelectList(db.FundingTypes, "FundingTypeID", "Title", project.FundingTypeID); ViewBag.IFISectionTypeID = new SelectList(db.IFISectionTypes, "IFISectionTypeID", "Title", project.IFISectionTypeID); ViewBag.LOBCategoryID = new SelectList(db.LOBCategories, "LOBCategoryID", "Title", project.LOBCategoryID); ViewBag.NeedTypeID = new SelectList(db.NeedTypes, "NeedTypeID", "Title", project.NeedTypeID); ViewBag.StatusTypeID = new SelectList(db.StatusTypes, "StatusTypeID", "Title", project.StatusTypeID);*/ return View(projectToUpdate); }