aktualisieren seite eingebaut
This commit is contained in:
@@ -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]
|
||||
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.Name</td>
|
||||
<td>@fahrzeug.GetType()</td>
|
||||
<td>
|
||||
<a asp-action=Aktualisieren asp-controller="Fahrzeug" asp-route-id=@fahrzeug.Id>Aktualisieren</a>
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action=Loesche asp-controller="Fahrzeug" asp-route-id=@fahrzeug.Id>Löschen</a>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user