aktualisieren seite eingebaut
This commit is contained in:
@@ -34,6 +34,30 @@ public class FahrzeugRepository
|
|||||||
return fahrzeugs;
|
return fahrzeugs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FahrzeugDTO GetFahrzeugByID(int id)
|
||||||
|
{
|
||||||
|
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
||||||
|
datenbankVerbindung.Open();
|
||||||
|
|
||||||
|
const string query = "SELECT id, fahrzeug_name, fahrzeug_typ FROM fahrzeuge WHERE id = @fahrzeug_id;";
|
||||||
|
using var kommando = new MySqlCommand(query, datenbankVerbindung);
|
||||||
|
kommando.Parameters.AddWithValue("@fahrzeug_id", id);
|
||||||
|
var reader = kommando.ExecuteReader();
|
||||||
|
|
||||||
|
List<FahrzeugDTO> fahrzeugs = new();
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
var fahrzeug = new FahrzeugDTO();
|
||||||
|
fahrzeug.Id = reader.GetInt32(0);
|
||||||
|
fahrzeug.Name = reader.GetString(1);
|
||||||
|
fahrzeug.Typ = reader.GetString(2);
|
||||||
|
|
||||||
|
fahrzeugs.Add(fahrzeug);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fahrzeugs[0];
|
||||||
|
}
|
||||||
|
|
||||||
public void FuegeFahrzeugEin(string fahrzeugName, string fahrzeugTyp)
|
public void FuegeFahrzeugEin(string fahrzeugName, string fahrzeugTyp)
|
||||||
{
|
{
|
||||||
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
||||||
@@ -46,6 +70,18 @@ public class FahrzeugRepository
|
|||||||
kommando.ExecuteNonQuery();
|
kommando.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AktualisiereFahrzeug(int id, string fahrzeugName, string fahrzeugTyp)
|
||||||
|
{
|
||||||
|
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
||||||
|
datenbankVerbindung.Open();
|
||||||
|
const string query = "UPDATE fahrzeuge SET fahrzeug_name = @fahrzeug_name, fahrzeug_typ = @fahrzeug_typ WHERE id = @id;";
|
||||||
|
using var kommando = new MySqlCommand(query, datenbankVerbindung);
|
||||||
|
kommando.Parameters.AddWithValue("@fahrzeug_name", fahrzeugName);
|
||||||
|
kommando.Parameters.AddWithValue("@fahrzeug_typ", fahrzeugTyp);
|
||||||
|
kommando.Parameters.AddWithValue("@id", id);
|
||||||
|
kommando.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
public void LoescheFahrzeug(int id)
|
public void LoescheFahrzeug(int id)
|
||||||
{
|
{
|
||||||
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
||||||
|
|||||||
@@ -55,6 +55,43 @@ public class FahrzeugController : Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Aktualisieren(int id)
|
||||||
|
{
|
||||||
|
if (ModelState.IsValid)
|
||||||
|
{
|
||||||
|
string connectionString = this.GetConnectionString();
|
||||||
|
var repository = new FahrzeugRepository(connectionString);
|
||||||
|
var fahrzeug = repository.GetFahrzeugByID(id);
|
||||||
|
var model = new FahrzeugAktualisierenModel();
|
||||||
|
model.Id = fahrzeug.Id;
|
||||||
|
model.Type = fahrzeug.Typ;
|
||||||
|
model.Name = fahrzeug.Name;
|
||||||
|
return View(model);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Aktualisieren(FahrzeugAktualisierenModel model)
|
||||||
|
{
|
||||||
|
if (ModelState.IsValid && !string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Type))
|
||||||
|
{
|
||||||
|
string connectionString = this.GetConnectionString();
|
||||||
|
var repository = new FahrzeugRepository(connectionString);
|
||||||
|
int id = (int)model.Id;
|
||||||
|
repository.AktualisiereFahrzeug(id, model.Name, model.Type);
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return View(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult Loesche(int id)
|
public IActionResult Loesche(int id)
|
||||||
{
|
{
|
||||||
|
|||||||
22
FahrzeugeMVC/Models/FahrzeugAktualisierenModel.cs
Normal file
22
FahrzeugeMVC/Models/FahrzeugAktualisierenModel.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using FahrzeugDatenBank;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace FahrzeugeMVC.Models;
|
||||||
|
|
||||||
|
public class FahrzeugAktualisierenModel
|
||||||
|
{
|
||||||
|
[Required]
|
||||||
|
public int? Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string? Name { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string? Type { get; set; }
|
||||||
|
|
||||||
|
public List<SelectListItem> FahrzeugTypen { get; private set; } = new()
|
||||||
|
{
|
||||||
|
new SelectListItem("Auto", "Auto", true),
|
||||||
|
new SelectListItem("Motorrad", "Motorrad"),
|
||||||
|
new SelectListItem("Fahrrad", "Fahrrad")
|
||||||
|
};
|
||||||
|
}
|
||||||
22
FahrzeugeMVC/Views/Fahrzeug/Aktualisieren.cshtml
Normal file
22
FahrzeugeMVC/Views/Fahrzeug/Aktualisieren.cshtml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
@model FahrzeugAktualisierenModel
|
||||||
|
|
||||||
|
<p>Aktualisiere das Fahrzeug:</p>
|
||||||
|
|
||||||
|
<form asp-controller="Fahrzeug" asp-action="Aktualisieren">
|
||||||
|
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label class="control-label">Name</label>
|
||||||
|
<input asp-for="Name" class="form-control"/>
|
||||||
|
<span asp-validation-for="Name" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="control-label">Type</label>
|
||||||
|
<select asp-for="Type" asp-items="Model.FahrzeugTypen" class="form-control"></select>
|
||||||
|
<span asp-validation-for="Type" class="text-danger"></span>
|
||||||
|
</div>
|
||||||
|
<br/>
|
||||||
|
<div class="form-group">
|
||||||
|
<input type="submit" value="Submit" class="btn bg-primary" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
@@ -24,6 +24,9 @@
|
|||||||
<td>@fahrzeug.Id</td>
|
<td>@fahrzeug.Id</td>
|
||||||
<td>@fahrzeug.Name</td>
|
<td>@fahrzeug.Name</td>
|
||||||
<td>@fahrzeug.GetType()</td>
|
<td>@fahrzeug.GetType()</td>
|
||||||
|
<td>
|
||||||
|
<a asp-action=Aktualisieren asp-controller="Fahrzeug" asp-route-id=@fahrzeug.Id>Aktualisieren</a>
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a asp-action=Loesche asp-controller="Fahrzeug" asp-route-id=@fahrzeug.Id>Löschen</a>
|
<a asp-action=Loesche asp-controller="Fahrzeug" asp-route-id=@fahrzeug.Id>Löschen</a>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user