public ActionResult Create( [Bind (Include = "Name,StartDate,EndDate,Description,Budget,SelfFinancing, MultiplePartners, Owner, AggregatedBudget,FunderId,ProgramId,PrimaryFocus,SecondaryFocus,Person,Responsible,RespNo,ProjectLink, Remark")] Project project, HttpPostedFileBase uploadApp) { try { if (ModelState.IsValid) { if (uploadApp != null && uploadApp.ContentLength > 0) { var application = new File { FileName = uploadApp.FileName, FileType = FileType.application, ContentType = uploadApp.ContentType, }; using (var reader = new System.IO.BinaryReader(uploadApp.InputStream)) { application.Content = reader.ReadBytes(uploadApp.ContentLength); } project.Files = new List<File> { application }; } //project.Id = Guid.NewGuid(); -- not necessary as set in model annotations: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] //Set the section, if RespNo is given at creation if (project.RespNo != null) { project.SetSection(project.RespNo); } db.AProjects.Add(project); db.SaveChanges(); return RedirectToAction("Index"); } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Kunne ikke gemme ændringer. Venligst forsøg igen."); } //ViewBag.PersonId = new SelectList(db.People, "PersonId", "FirstName", project.PersonId); ViewBag.ProgramId = new SelectList(db.Programs, "ProgramId", "ProgramName", project.ProgramId); ViewBag.FunderId = new SelectList(db.Funders, "FunderId", "Name", project.FunderId); ViewBag.PrimaryFocus = new SelectList(Initiative.ElementAt(0).Value); ViewBag.SecondaryFocus = new SelectList(Initiative.ElementAt(1).Value); return View(project); }
public ActionResult EditPost(Guid? id, HttpPostedFileBase uploadApp, HttpPostedFileBase uploadResponse) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } //Find the project var projectToUpdate = db.AProjects.Find(id); // Using TryUpdateModel instead of binding to prevent overposting.(the Bind attribute clears out //any pre-existing data in fields not listed in the Include parameter) Properties added will be updated if there is no input, it will // be set to null. if (TryUpdateModel(projectToUpdate, "", new string[] { "Name", "Status","StartDate", "EndDate", "Description", "Budget", "SelfFinancing", "MultiplePartners", "Owner", "AggregatedBudget", "Person", "FunderId", "ProgramId", "PrimaryFocus", "SecondaryFocus", "Responsible", "RespNo", "ProjectNumber", "ExtProjectNumber", "ProjectLink", "Remark" })) { try { // Upload application if (uploadApp != null && uploadApp.ContentLength > 0) { if (projectToUpdate.Files.Any(p => p.FileType == FileType.application)) { db.Files.Remove(projectToUpdate.Files.First(f => f.FileType == FileType.application)); } var application = new File { FileName = uploadApp.FileName, FileType = FileType.application, ContentType = uploadApp.ContentType }; using (var reader = new System.IO.BinaryReader(uploadApp.InputStream)) { application.Content = reader.ReadBytes(uploadApp.ContentLength); } if (projectToUpdate.Files != null) { projectToUpdate.Files.Add(application); } else { projectToUpdate.Files = new List<Models.File> { application }; } } // Upload response if (uploadResponse != null && uploadResponse.ContentLength > 0) { if (projectToUpdate.Files.Any(p => p.FileType == FileType.response)) { db.Files.Remove(projectToUpdate.Files.First(f => f.FileType == FileType.response)); } var response = new File { FileName = uploadResponse.FileName, FileType = FileType.response, ContentType = uploadResponse.ContentType }; using (var reader = new System.IO.BinaryReader(uploadResponse.InputStream)) { response.Content = reader.ReadBytes(uploadResponse.ContentLength); } if (projectToUpdate.Files != null) { projectToUpdate.Files.Add(response); } else { projectToUpdate.Files = new List<Models.File> { response }; } } //Update section if (projectToUpdate.RespNo != null) { projectToUpdate.SetSection(projectToUpdate.RespNo); } db.SaveChanges(); return RedirectToAction("Index"); } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Kunne ikke gemme ændringer. Venligst forsøg igen."); } } //ViewBag.PersonId = new SelectList(db.People, "PersonId", "FirstName", projectToUpdate.PersonId); ViewBag.ProgramId = new SelectList(db.Programs, "ProgramId", "ProgramName", projectToUpdate.ProgramId); ViewBag.FunderId = new SelectList(db.Funders, "FunderId", "Name", projectToUpdate.FunderId); ViewBag.PrimaryFocus = new SelectList(Initiative.ElementAt(0).Value, projectToUpdate.PrimaryFocus); ViewBag.SecondaryFocus = new SelectList(Initiative.ElementAt(1).Value, projectToUpdate.SecondaryFocus); return View(projectToUpdate); }