Created
February 26, 2014 10:54
-
-
Save DerAlbertCom/9227496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics.Eventing.Reader; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Training.Web.Data; | |
using Training.Web.Entities; | |
namespace Training.Web.Controllers | |
{ | |
public class EditPersonController : Controller | |
{ | |
public ActionResult Index(string spalte = "", string richtung ="asc") | |
{ | |
ViewBag.Spalte = spalte.ToLower(); | |
richtung = richtung.ToLower(); | |
ViewBag.Richtung = richtung; | |
var personen = EntityStore.Current.Personen; | |
switch (spalte.ToLower()) | |
{ | |
case "anrede": | |
personen = OrderPerson(personen, p => p.Anrede, richtung); | |
break; | |
case "vorname": | |
personen = OrderPerson(personen, p => p.Vorname, richtung); | |
break; | |
case "nachname": | |
personen = OrderPerson(personen, p => p.Nachname, richtung); | |
break; | |
} | |
return View(personen); | |
} | |
private static IOrderedEnumerable<Person> OrderPerson( | |
IEnumerable<Person> personen, | |
Func<Person, string> keySelector, | |
string richtung) | |
{ | |
if (richtung == "asc") | |
{ | |
return personen.OrderBy(keySelector); | |
} | |
return personen.OrderByDescending(keySelector); | |
} | |
[HttpGet] | |
public ActionResult Create() | |
{ | |
return View(new Person()); | |
} | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Create(Person model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
EntityStore.Current.Add(model); | |
return RedirectToAction("Index"); | |
} | |
model.Anrede = "Frau"; | |
return View(model); | |
} | |
public ActionResult Edit(int id) | |
{ | |
return FindPersonAndShowView(id); | |
} | |
private static Person FindPerson(int id) | |
{ | |
return EntityStore.Current.Personen.SingleOrDefault(p => p.Id == id); | |
} | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Edit(Person model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var person = FindPerson(model.Id); | |
if (person == null) | |
{ | |
return HttpNotFound(); | |
} | |
person.Anrede = model.Anrede; | |
person.Vorname = model.Vorname; | |
person.Nachname = model.Nachname; | |
return RedirectToAction("Index"); | |
} | |
return View(); | |
} | |
public ActionResult Details(int id) | |
{ | |
return FindPersonAndShowView(id); | |
} | |
private ActionResult FindPersonAndShowView(int id) | |
{ | |
var person = FindPerson(id); | |
if (person == null) | |
{ | |
return RedirectToAction("Index"); | |
} | |
return View(person); | |
} | |
public ActionResult Delete(int id) | |
{ | |
return FindPersonAndShowView(id); | |
} | |
[HttpPost] | |
[ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public ActionResult DeletePost(int id) | |
{ | |
var person = FindPerson(id); | |
if (person == null) | |
{ | |
return RedirectToAction("Index"); | |
} | |
EntityStore.Current.Remove(person); | |
return RedirectToAction("Index"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model IEnumerable<Training.Web.Entities.Person> | |
@using Training.Web.Helpers | |
@{ | |
ViewBag.Title = "Index"; | |
ViewData[ViewBag.Spalte + "Class"] = ViewBag.Richtung; | |
ViewData[ViewBag.Spalte + "NextSort"] = ViewBag.Richtung == "asc" ? "desc" : "asc"; | |
} | |
<h2>Index</h2> | |
<p> | |
@Html.ActionLink("Create New", "Create") | |
</p> | |
<table> | |
<tr> | |
<th class="@ViewData["anredeClass"]"> | |
@Html.SortActionLink(p=>p.Anrede) | |
@*@Html.ActionLink("Anrede", "Index", new { spalte = "anrede", richtung = ViewBag.anredeNextSort })*@ | |
</th> | |
<th class="@ViewData["vornameClass"]"> | |
@Html.SortActionLink(p => p.Vorname) | |
@*@Html.ActionLink("Vorname", "Index", new { spalte = "vorname", richtung = ViewBag.vornameNextSort })*@ | |
</th> | |
<th class="@ViewData["nachnameClass"]"> | |
@Html.SortActionLink(p => p.Vorname) | |
@*@Html.ActionLink("Nachname", "Index", new { spalte = "nachname", richtung = ViewBag.nachnameNextSort })*@ | |
</th> | |
<th></th> | |
</tr> | |
@foreach (var item in Model) | |
{ | |
<tr onclick="alert('ding');"> | |
<td> | |
@Html.DisplayFor(modelItem => item.Anrede) | |
</td> | |
<td> | |
@Html.DisplayFor(modelItem => item.Vorname) | |
</td> | |
<td> | |
@Html.DisplayFor(modelItem => item.Nachname) | |
</td> | |
<td> | |
@Html.ActionLink("Edit", "Edit", new {id = item.Id}) | | |
@Html.ActionLink("Details", "Details", new {id = item.Id}) | | |
@Html.ActionLink("Delete", "Delete", new {id = item.Id}, new {data_postit = true}) | |
</td> | |
</tr> | |
} | |
</table> | |
<form id="csrf"> | |
@Html.AntiForgeryToken() | |
</form> | |
@section scripts { | |
<script> | |
//$(function() { | |
// $("a[data-postit=True]").on('click', function(event) { | |
// event.stopPropagation(); | |
// event.preventDefault(); | |
// var action = $(this).attr('href'); | |
// alert(action); | |
// }); | |
//}); | |
function postTheLink(event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
var action = $(this).attr('href'); | |
var data = $('form').serialize(); | |
$.post(action, data, redirectToIndex); | |
}; | |
function redirectToIndex(a,b,c) { | |
window.location.pathname = '@Url.Action("Index")'; | |
} | |
function linkDeleteButtons() { | |
$("a[data-postit=True]").on('click', postTheLink); | |
} | |
$(linkDeleteButtons); | |
</script> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting Code
Online MVC Training
Online MVC Training from India