public async Task <IActionResult> Edit(int id, [Bind("Id,Type,Naam,Prijs,Merk,Geheugen,Aantal,Afbeelding,Aantal_gekocht,CategorieId")] Spelcomputer spelcomputer) { if (id != spelcomputer.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(spelcomputer); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpelcomputerExists(spelcomputer.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategorieId"] = new SelectList(_context.Categories, "Id", "Id", spelcomputer.CategorieId); return(View(spelcomputer)); }
public async Task <IActionResult> Create([Bind("Id,Type,Naam,Prijs,Merk,Geheugen,Aantal,Afbeelding,Aantal_gekocht,CategorieId")] Spelcomputer spelcomputer) { if (ModelState.IsValid) { _context.Add(spelcomputer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategorieId"] = new SelectList(_context.Categories, "Id", "Id", spelcomputer.CategorieId); return(View(spelcomputer)); }
public async Task <IActionResult> PostSpelcomputer(List <IFormFile> files) { long size = files.Sum(f => f.Length); string error = ""; // full path to file in temp location var filePath = Path.GetTempFileName(); foreach (var formFile in files) { if (formFile.Length > 0) { if (formFile.FileName.EndsWith(".csv")) { using (var stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); } var sr = new StreamReader(formFile.OpenReadStream()); try { while (!sr.EndOfStream) { var line = sr.ReadLine(); var data = line.Split(new[] { ',' }); var spelcomputer = new Spelcomputer() { Type = data[0], Naam = data[1], Prijs = /* Veranderen naar Float?*/ int.Parse(data[2]), Merk = data[3], Geheugen = int.Parse(data[4]), Aantal = int.Parse(data[5]), Afbeelding = data[6], Aantal_gekocht = int.Parse(data[7]), CategorieId = 1 }; _context.Spelcomputers.Add(spelcomputer); } } catch (FormatException ex) { System.Console.WriteLine(ex.Message); System.Console.WriteLine("CATCHED ERRRRRRROR-------------------------------------"); error = "ERROR"; } catch (IndexOutOfRangeException e) { System.Console.WriteLine(e.Message); System.Console.WriteLine("ERRRRRRRRRRRRRRRRORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"); error = "ERROR"; } _context.SaveChanges(); } else { return(RedirectToAction("Error")); } } } if (error == "ERROR") { return(RedirectToAction("Error")); } // process uploaded files // Don't rely on or trust the FileName property without validation. return(RedirectToAction("Succes")); }