コード例 #1
0
        // GET: BackOffice/Authors/Create
        public ActionResult Create()
        {
            var author = new Author();

            author.Translations.Add(new AuthorTranslation
                {
                    LanguageCode = LanguageDefinitions.DefaultLanguage
                });

            var model = new AuthorEditViewModel
            {
                Author = author
            };

            return View(model);
        }
コード例 #2
0
        public async Task<ActionResult> Create(AuthorEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.ImageUpload != null)
                {
                    var newName = Guid.NewGuid().ToString() + ".jpg";

                    FileUploadHelper.SaveImage(model.ImageUpload.InputStream,
                        400, 400,
                        Server.MapPath("~/Public/Authors/") + newName,
                        FitMode.Crop);

                    model.Author.PictureFileName = newName;
                }

                db.Add(model.Author);
                await db.SaveChangesAsync();

                return RedirectToAction("Index");
            }

            return View(model);
        }
コード例 #3
0
        public async Task<ActionResult> Edit(AuthorEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                db.Update(model.Author);

                if (model.ImageUpload != null)
                {
                    var logo = db.GetValueFromDb(model.Author, c => c.PictureFileName);

                    if (logo == null)
                    {
                        model.Author.PictureFileName =
                            Guid.NewGuid().ToString() + ".jpg";

                        logo = model.Author.PictureFileName;
                    }

                    FileUploadHelper.SaveImage(model.ImageUpload.InputStream,
                        400, 400,
                        Server.MapPath("~/Public/Authors/") + logo,
                        FitMode.Crop);
                }
                else
                {
                    db.ExcludeFromUpdate(model.Author, c => new { c.PictureFileName });
                }



                // Update each translation.
                foreach (var t in model.Author.Translations)
                {
                    db.UpdateTranslation(t);
                }

                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(model);
        }