Exemple #1
0
        public async Task <IActionResult> CreateBook(GiftUploadViewModel model)
        {
            ModelState.Remove("UserId");

            var user = await GetCurrentUserAsync();

            //If you want to check errors in model state use the code below:
            var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (ModelState.IsValid)
            {
                model.GiftIdeas.Book.User   = user;
                model.GiftIdeas.Book.UserId = user.Id;
                await UploadImage(model.ImageFile);

                model.GiftIdeas.Book.ImagePath = model.ImageFile.FileName;
                _context.Add(model.GiftIdeas.Book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BookTypeId"] = new SelectList(_context.BookType, "BookTypeId", "Description", model.GiftIdeas.BookTypeId);
            ViewData["UserId"]     = new SelectList(_context.ApplicationUsers, "Id", "Id", model.GiftIdeas.Book.UserId);
            return(View(model));
        }
Exemple #2
0
        // GET: GiftIdeas/Create
        public IActionResult CreateBook()
        {
            GiftUploadViewModel model = new GiftUploadViewModel();

            model.GiftIdeas        = new GiftIdeas();
            ViewData["BookTypeId"] = new SelectList(_context.BookType, "BookTypeId", "Description");
            return(View(model));
        }