// GET: User/Edit/5 (View laden und mit Informationen aus der Datenbank befüllen)
public ActionResult Edit(int? id)
{
User user;
// Falls ID nicht vorhanden -> Error
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
// Datenbankverbindung öffnen
SQLiteConnection connection = DbConnect();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = string.Format("SELECT * FROM db_User WHERE Id = " + id);
using (SQLiteDataReader reader = command.ExecuteReader())
{
reader.Read();
user = new User
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
};
}
}
// Informationen über User an den View übergeben
return View(user)
}
// POST: User/Edit/5 (Aktualisierte Daten in Datenbank schreiben)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name")]User user)
{
SQLiteConnection connection = DbConnect();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = string.Format("UPDATE db_User SET Name = '{1}' WHERE Id = {0}",
user.Id,
user.Name
);
command.ExecuteNonQuery();
}
return RedirectToAction("Index");
}
|