public ActionResult AddImage(HttpPostedFileBase uploadedFile) { // uploadedFileパラメータは、Get用のアクションメソッドと区別するためのダミーとする。 // 画像ファイルの扱いはWebImageクラスを使用する。 var image = WebImage.GetImageFromRequest(); if (image == null) { // 画像ファイルとして不適切 return RedirectToAction("Index"); } // TODO: トランザクション // 現在のプロジェクトと一緒に、実行ユーザーと所属企業もAttachされる。 db.Configuration.ProxyCreationEnabled = false; db.Projects.Attach(this.Project); db.Entry(this.Project).Reference(x => x.ProjectPage).Query().Include(x => x.PageContents).Load(); var projectPage = this.Project.ProjectPage; if (projectPage == null) { projectPage = new ProjectPage { Created = ctx.Now }; this.Project.ProjectPage = projectPage; } var document = new Document() { ID = Guid.NewGuid(), BinaryData = image.GetBytes(), FileExtension = "." + image.ImageFormat, Uploaded = ctx.Now, User = ctx.CurrentUser }; db.Documents.Add(document); var content = new PageContent { ProjectPage = projectPage, DocumentID = document.ID }; projectPage.PageContents.Add(content); db.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult AddSection([Bind(Include = "title")] Models.ProjectPages.Section section) { if (section == null) { return RedirectToAction("Index"); } if (string.IsNullOrEmpty(section.title)) { return RedirectToAction("Index"); } // TODO: トランザクション // 現在のプロジェクトと一緒に、実行ユーザーと所属企業もAttachされる。 db.Configuration.ProxyCreationEnabled = false; db.Projects.Attach(this.Project); // PageContentsは不要のため、ProjectPageだけ読み取る。 db.Entry(this.Project).Reference(x => x.ProjectPage).Load(); var projectPage = this.Project.ProjectPage; if (projectPage == null) { projectPage = new ProjectPage { Created = ctx.Now }; this.Project.ProjectPage = projectPage; } var pageBody = projectPage.PageBody ?? new Models.ProjectPages.PageBody(); if (pageBody.sections == null) { pageBody.sections = new List<Models.ProjectPages.Section>(); } pageBody.sections.Add(section); projectPage.UpdatePageBody(pageBody); db.SaveChanges(); return RedirectToAction("Index"); }