public async Task<ActionResult> Create(int documentId = 0, int keywordId = 0, int classificationId = 0) { var image = new Image(); if (db.Set<Document>().Any(d => d.Id == documentId)) { image.DocumentId = documentId; image.ImageCode = CodeGenerator.SuggestImageCode(documentId); } if (keywordId != 0) { image.Keywords = await db.Set<Keyword>().Where(k => k.Id == keywordId).ToListAsync(); } if (classificationId != 0) { image.ClassificationId = classificationId; } image.Translations.Add(new ImageTranslation { LanguageCode = LanguageDefinitions.DefaultLanguage }); var model = new ImageEditViewModel(image); model.PopulateDropDownLists(db.Set<Document>(), db.Set<Classification>(), db.Set<Keyword>()); return View(model); }
public async Task<ActionResult> Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var image = await db.GetByIdAsync(id); if (image == null) { return HttpNotFound(); } var model = new ImageEditViewModel(image); model.PopulateDropDownLists(db.Set<Document>(), db.Set<Classification>(), db.Set<Keyword>()); return View(model); }
public async Task<ActionResult> Edit(ImageEditViewModel model) { if (DoesCodeAlreadyExist(model.Image)) { ModelState.AddModelError("Image.ImageCode", ImageStrings.CodeAlreadyExists); } if (ModelState.IsValid) { // "Force-load" the image and the keywords. await db.ForceLoadAsync(model.Image, i => i.Keywords); db.Update(model.Image); model.Image.Keywords = db.Set<Keyword>() .Where(k => model.KeywordIds.Contains(k.Id)).ToList(); foreach (var t in model.Image.Translations) { db.UpdateTranslation(t); } if (model.ImageUpload != null) { var fileName = db.GetValueFromDb(model.Image, i => i.ImageUrl) ?? Guid.NewGuid().ToString(); var path = Server.MapPath("~/Public/Images/"); Directory.CreateDirectory(path); FileUploadHelper.GenerateVersions(model.ImageUpload.InputStream, path + fileName); model.Image.ImageUrl = fileName; } else { db.ExcludeFromUpdate(model.Image, i => new { i.ImageUrl }); } await db.SaveChangesAsync(); return RedirectToAction("Index"); } model.PopulateDropDownLists(db.Set<Document>(), db.Set<Classification>(), db.Set<Keyword>()); return View(model); }
public async Task<ActionResult> Create(ImageEditViewModel model) { if (DoesCodeAlreadyExist(model.Image)) { ModelState.AddModelError("Image.ImageCode", ImageStrings.CodeAlreadyExists); } if (ModelState.IsValid) { if (model.ImageUpload != null) { var fileName = Guid.NewGuid().ToString(); var path = Server.MapPath("~/Public/Images/"); FileUploadHelper.GenerateVersions(model.ImageUpload.InputStream, path + fileName); model.Image.ImageUrl = fileName; } model.Image.Keywords = db.Set<Keyword>() .Where(kw => model.KeywordIds.Contains(kw.Id)) .ToList(); db.Add(model.Image); await db.SaveChangesAsync(); return RedirectToAction("Index"); } model.PopulateDropDownLists(db.Set<Document>(), db.Set<Classification>(), db.Set<Keyword>()); return View(model); }