public async Task <IActionResult> Create(TrainingDefinitionViewModel trainingDefinition, IFormFile file) { if (ModelState.IsValid) { //trainingDefinition.TrainingId = id; if (file == null && file.Length == 0) { return(Content("file not selected")); } var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); var filePath = Path.Combine(_appEnvironment.WebRootPath, "images\\TrainingMaterial"); using (var fileStream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); trainingDefinition.TraningMaterial = fileName; } _context.Add(trainingDefinition.GetModel()); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } trainingDefinition.loadLists(_context); //ViewData["JobId"] = new SelectList(_context.JobTitle, "Id", "Name", trainingDefinition.JobId); //ViewData["TrainingId"] = new SelectList(_context.TrainingDetails, "Id", "Id", trainingDefinition.TrainingId); return(View(trainingDefinition)); }
// GET: TrainingDefinitions/Create /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public IActionResult Create(int id) { TrainingDefinitionViewModel vm; vm = new TrainingDefinitionViewModel(_context); //vm.TrainingId = id; return(View(vm)); //ViewData["JobId"] = new SelectList(_context.JobTitle, "Id", "Name"); //ViewData["TrainingId"] = new SelectList(_context.TrainingDetails, "Id", "Id"); //return View(); }
public async Task <IActionResult> Edit(int id, TrainingDefinitionViewModel trainingDefinition, IFormFile file) { if (id != trainingDefinition.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (file == null && file.Length == 0) { return(Content("file not selected")); } var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); var filePath = Path.Combine(_appEnvironment.WebRootPath, "images\\TrainingMaterial"); using (var fileStream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); trainingDefinition.TraningMaterial = fileName; } _context.Update(trainingDefinition); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TrainingDefinitionExists(trainingDefinition.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } //ViewData["JobId"] = new SelectList(_context.JobTitle, "Id", "Name", trainingDefinition.JobId); //ViewData["TrainingId"] = new SelectList(_context.TrainingDetails, "Id", "Id", trainingDefinition.TrainingId); //ViewBag.TrainingId = trainingDefinition.TrainingId; trainingDefinition.loadLists(_context); return(View(trainingDefinition)); }
// GET: TrainingDefinitions/Edit/5 /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <IActionResult> Edit(int?id) { TrainingDefinitionViewModel vm; if (id == 0) { vm = new TrainingDefinitionViewModel(_context); } else { var training = await _context.TrainingDefinition.FindAsync(id); if (training == null) { return(NotFound()); } vm = new TrainingDefinitionViewModel(_context, training); //ViewBag.TrainingId = training.TrainingId; } return(View(vm)); //if (id == null) //{ // return NotFound(); //} //var trainingDefinition = await _context.TrainingDefinition.FindAsync(id); //if (trainingDefinition == null) //{ // return NotFound(); //} //ViewData["JobId"] = new SelectList(_context.JobTitle, "Id", "Name", trainingDefinition.JobId); //ViewData["TrainingId"] = new SelectList(_context.TrainingDetails, "Id", "Id", trainingDefinition.TrainingId); //return View(trainingDefinition); }