コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,Title,VideoPath,Description,DateAdded,ProductId, File")] TutorialCreateViewModel viewModel, IFormFile video)
        {
            ModelState.Remove("User");
            ModelState.Remove("UserId");
            var user = await GetCurrentUserAsync();

            if (ModelState.IsValid)
            {
                var tutorial = new Tutorial()
                {
                    Title       = viewModel.Title,
                    Description = viewModel.Description,
                };
                if (viewModel.File != null && viewModel.File.Length > 0)
                {
                    var fileName = Path.GetFileName(viewModel.File.FileName);                                  //getting path of actual file name
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\videos", fileName); //creating path combining file name w/ www.root\\images directory
                    using (var fileSteam = new FileStream(filePath, FileMode.Create))                          //using filestream to get the actual path
                    {
                        await viewModel.File.CopyToAsync(fileSteam);
                    }
                    tutorial.VideoPath = fileName;
                }
                tutorial.UserId = user.Id;
                _context.Add(tutorial);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", viewModel.UserId);
            return(View(viewModel));
        }
コード例 #2
0
        // GET: Tutorials/Create
        public async Task <IActionResult> CreateAsync()
        {
            var viewModel = new TutorialCreateViewModel();
            var user      = await GetCurrentUserAsync();

            ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id");
            return(View(viewModel));
        }