Beispiel #1
0
        public async Task <IActionResult> Edit(int id, AuthorEditViewModel viewModel)
        {
            if (id != viewModel.Author.AuthorId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewModel.Author);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(viewModel.Author.AuthorId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel.Author));
        }
Beispiel #2
0
        public ActionResult Edit(AuthorEditViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new ModelNotValidException("Podaci nisu ispravno uneseni");
                }

                _repository.UpdateAuthor(viewModel);
            }
            catch (Exception e)
            {
                if (e is ModelNotValidException)
                {
                    TempData["ErrorMsg"] = e.Message;
                }
                else
                {
                    TempData["ErrorMsg"] = "Pogreška prilikom uređivanja autora";
                }

                viewModel.NumberOfBooks = _repository.GetBooks().Count(x => x.AuthorId == viewModel.Id);
                return(View(viewModel));
            }

            return(RedirectToAction("Details", new { id = viewModel.Id }));
        }
Beispiel #3
0
        public async Task <IActionResult> Edit(AuthorEditViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }
            var model = await _context.Authors
                        .Include(x => x.BookAuthors)
                        .FirstOrDefaultAsync(x => x.Id == viewModel.Id);

            if (model != null)
            {
                model.Name = viewModel.Name;
                model.BookAuthors.Clear();
                model.BookAuthors.AddRange(viewModel.BooksIds
                                           .Select(bookId => new BookAuthor {
                    BookId = bookId, AuthorId = viewModel.Id
                }));
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(NotFound());
            }
        }
        public ActionResult EditAuthor(string id, AuthorEditViewModel model)
        {
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            // 送信されてきたデータを再チェック
            if (ModelState.IsValid == false)
            {
                // 前画面を返す
                // ID フィールドがロストしているので補完する
                model.AuthorId = id;
                return(View(model));
            }

            model.AuthorId = id;

            // データベースに登録を試みる
            using (var pubs = new PubsEntities())
            {
                var target = pubs.Authors.Where(a => a.AuthorId == model.AuthorId).FirstOrDefault();
                target.AuthorFirstName = model.AuthorFirstName;
                target.AuthorLastName  = model.AuthorLastName;
                target.Phone           = model.Phone;
                target.State           = model.State;

                pubs.SaveChanges();
            }
            // 一覧画面に帰る
            return(RedirectToAction("ListAuthors"));
        }
        public ActionResult EditAuthor(string id)
        {
            // 当該著者 ID のデータを読み取る
            Author editAuthor = null;

            using (var pubs = new PubsEntities())
            {
                editAuthor = pubs.Authors.FirstOrDefault(a => a.AuthorId == id);
            }

            // View に引き渡すデータを準備する
            var vm = new AuthorEditViewModel()
            {
                AuthorId        = editAuthor.AuthorId,
                AuthorFirstName = editAuthor.AuthorFirstName,
                AuthorLastName  = editAuthor.AuthorLastName,
                Phone           = editAuthor.Phone,
                State           = editAuthor.State
            };

            // View にデータを引き渡すにあたり、入力データと周辺データを分けておく。
            // (ViewModel に周辺データを入れることで、ViewModel を完全にフォームモデルに一致させるように設計)
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            return(View(vm));
        }
Beispiel #6
0
        public IActionResult Create()
        {
            var viewModel = new AuthorEditViewModel()
            {
                Heading = "Add an Author"
            };

            return(View("AuthorForm", viewModel));
        }
Beispiel #7
0
        //Update author
        public void UpdateAuthor(AuthorEditViewModel viewModel)
        {
            var author = GetAuthorById(viewModel.Id);

            author.Id         = viewModel.Id;
            author.Name       = viewModel.Name;
            author.Surname    = viewModel.Surname;
            author.DateBorned = DateTime.Parse(viewModel.DateOfBirth);
        }
Beispiel #8
0
        public IActionResult Update(AuthorEditViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("AuthorForm", viewModel));
            }

            //UpdateAuthor(viewModel.Id, authorDto);

            return(RedirectToAction("Index", "Authors"));
        }
Beispiel #9
0
        public IActionResult Edit(int id)
        {
            Author authorFromDb = _context.GetPodcastAuthor(id);

            AuthorEditViewModel vm = new AuthorEditViewModel()
            {
                Name      = authorFromDb.Name,
                FirstName = authorFromDb.FirstName,
            };

            return(View(vm));
        }
        public ActionResult AddAuthor(AuthorEditViewModel authorEditViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(authorEditViewModel));
            }

            var author = Mapper.Map <Author>(authorEditViewModel);

            _authorService.Create(author);

            return(RedirectToAction("Index"));
        }
        // 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));
        }
Beispiel #12
0
        public ActionResult Edit(int id)
        {
            var viewModel = new AuthorEditViewModel();

            try
            {
                var author = _repository.GetAuthorById(id);
                var books  = _repository.GetBooks().Where(x => x.AuthorId == author.Id).ToList();
                viewModel.PrepareData(author, books);
            }
            catch (Exception e)
            {
                TempData["ErrorMsg"] = "Greška prilikom dohvaćanja podataka za uređivanje";
                return(View("Authors"));
            }

            return(View(viewModel));
        }
        // GET: Authors/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            AuthorEditViewModel viewModel = new AuthorEditViewModel();

            viewModel.Author = await _context.Authors.FindAsync(id);

            if (viewModel.Author == null)
            {
                return(NotFound());
            }
            ViewData["ApplicationUserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", viewModel.Author.ApplicationUserId);
            return(View(viewModel));
        }
Beispiel #14
0
        public IActionResult Edit(int id, AuthorEditViewModel model)
        {
            if (!TryValidateModel(model))
            {
                return(View(model));
            }

            Author authorToUpdate = new Author()
            {
                Id        = id,
                Name      = model.Name,
                FirstName = model.FirstName,
            };

            _context.UpdatePodcastAuthor(id, authorToUpdate);

            return(RedirectToAction("Index"));
        }
        // GET: Authors/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var author = await _context.Authors.FindAsync(id);

            if (author == null)
            {
                return(NotFound());
            }
            AuthorEditViewModel viewModel = _mapper.Map <AuthorEditViewModel>(author);

            ViewData["GenderId"] = new SelectList(_context.Genders, "Id", "Name");

            return(PartialView("_EditPartial", viewModel));
        }
Beispiel #16
0
        public IActionResult Create(AuthorEditViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("AuthorForm", viewModel));
            }

            var authorDto = new AuthorDto()
            {
                FirstName   = viewModel.FirstName,
                LastName    = viewModel.LastName,
                DateOfBirth = viewModel.DateOfBirth,
                Genre       = viewModel.Genre
            };

            //AddAuthor(authorDto);

            return(RedirectToAction("Index", "Authors"));
        }
        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));
        }
        public async Task <IActionResult> Edit(long id, [Bind("Id,ImageFile,FirstName,LastName,MiddleName,GenderId,Address,EmailAddress,PhoneNumber,IsActive")] AuthorEditViewModel viewModel)
        {
            if (id != viewModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Author author = _mapper.Map <Author>(viewModel);

                    if (viewModel.ImageFile != null)
                    {
                        _fileUpload.DeleteFile(viewModel.ExistingImage, uploadImagePath);

                        var result = _fileUpload.SaveFile(viewModel.ImageFile, uploadImagePath);
                        author.Image = result.UniqueFileName;
                    }

                    _context.Update(author);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(viewModel.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                //return RedirectToAction(nameof(Index));
            }
            ViewData["GenderId"] = new SelectList(_context.Genders, "Id", "Name");
            return(PartialView("_EditPartial", viewModel));
        }
Beispiel #19
0
        // GET: Authors/Edit/5

        /* public async Task<IActionResult> Edit(int? id)
         * {
         *   if (id == null)
         *   {
         *       return NotFound();
         *   }
         *
         *   var author = await _context.Author.FindAsync(id);
         *   if (author == null)
         *   {
         *       return NotFound();
         *   }
         *   return View(author);
         * }*/


        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var author = await _context.Author
                         .Include(a => a.Books)
                         .FirstOrDefaultAsync(ae => ae.AuthorId == id);

            var viewModel = new AuthorEditViewModel
            {
                Author  = author,
                CanEdit = author.Books.Count == 0
            };

            if (author == null)
            {
                return(NotFound());
            }
            return(View(viewModel));
        }
        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));
        }
        public ActionResult AddAuthor()
        {
            var authorEditViewModel = new AuthorEditViewModel();

            return(View(authorEditViewModel));
        }