public async Task <IActionResult> Create(ToyUploadPictureViewModel model) { // Remove user from model validation as it is not submitted in the form. // Validation will fail is this is not in here 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.Toy.User = user; model.Toy.UserId = user.Id; await UploadImage(model.ImageFile); model.Toy.ImagePath = model.ImageFile.FileName; _context.Add(model.Toy); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ToyTypeId"] = new SelectList(_context.ToyType, "ToyTypeId", "Description", model.Toy.ToyTypeId); ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", model.Toy.UserId); return(View(model)); }
// GET: Books/Create public IActionResult Create() { ToyUploadPictureViewModel model = new ToyUploadPictureViewModel(); model.Toy = new Toy(); ViewData["ToyTypeId"] = new SelectList(_context.ToyType, "ToyTypeId", "Description"); ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id"); return(View(model)); }