public async Task <ActionResult> Edit(Document document)
        {
            if (DoesCodeAlreadyExist(document))
            {
                ModelState.AddModelError("CatalogCode", DocumentStrings.CodeAlreadyExists);
            }

            if (ModelState.IsValid)
            {
                foreach (var t in document.Translations)
                {
                    db.UpdateTranslation(t);
                }

                db.Update(document);

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            var model = new DocumentEditViewModel(document);
            await model.PopulateDropDownLists(db.Set <Author>(), db.Set <Collection>());

            return(View(model));
        }
        // GET: BackOffice/Documents/Create
        public async Task <ActionResult> Create(int?collectionId, int?authorId)
        {
            var doc = new Document();

            doc.Translations.Add(new DocumentTranslation
            {
                LanguageCode = LanguageDefinitions.DefaultLanguage
            });

            // Check for a collection.
            if (collectionId != null && db.Set <Collection>().Any(c => c.Id == collectionId))
            {
                doc.CatalogCode  = CodeGenerator.SuggestDocumentCode(collectionId.Value);
                doc.CollectionId = collectionId.Value;
            }

            // Check for an author.
            if (authorId != null && db.Set <Author>().Any(a => a.Id == authorId))
            {
                doc.AuthorId = authorId.Value;
            }

            var model = new DocumentEditViewModel(doc);
            await model.PopulateDropDownLists(db.Set <Author>(), db.Set <Collection>());

            return(View(model));
        }
        // GET: BackOffice/Documents/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Document document = await db.GetByIdAsync(id);

            if (document == null)
            {
                return(HttpNotFound());
            }

            document.Translations = document.Translations.ToList();

            var model = new DocumentEditViewModel(document);
            await model.PopulateDropDownLists(db.Set <Author>(), db.Set <Collection>());

            return(View(model));
        }