public ActionResult Edit(int Id) { using(var dataContext = new DataContext()) { var card = dataContext.Cards.FirstOrDefault(c => c.Id == Id); return PartialView(card); } }
public ActionResult Edit(int id, FormCollection formCollection) { using (var dataContext = new DataContext()) { var card = dataContext.Cards.Find(id); if (ModelState.IsValid) { try { UpdateModel(card); dataContext.SaveChanges(); return PartialView(card); } catch { return Content("something horrible happened. Sorry about that. Best to just move on."); } } return PartialView(card); } }
public ActionResult Index(string sortingAttribute) { if(sortingAttribute == null) sortingAttribute = "Status"; using(var dataContext = new DataContext()) { var sortableAttributes = dataContext.Attributes .Select(a => a.Key) .Distinct() .OrderBy(a => a) .ToArray(); var cards = dataContext.Cards .Select(c => new { Key = c.Attributes .Where(a => a.Key == sortingAttribute) .FirstOrDefault(), Card = c, }) .OrderBy(o => o.Key.DisplayOrder) .ToLookup( o => o.Key == null ? null : o.Key.Value, o => o.Card ); var viewModel = new Models.Cards.Index<string> { SortingAttribute = sortingAttribute, SortableAttributes = sortableAttributes, Cards = cards, }; return View(viewModel); } }
public ActionResult UpdateCardAttribute(int cardId, string attributeKey, string attributeValue) { using(var dataContext = new DataContext()) { var attribute = dataContext.Cards .Where(c => c.Id == cardId) .SelectMany(c => c.Attributes) .Where(a => a.Key == attributeKey) .FirstOrDefault(); if(attribute == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound); attribute.Value = attributeValue; dataContext.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } }
public ActionResult UpdateCardDisplayOrder(int cardId, int? previousCardId, string attributeKey) { using(var dataContext = new DataContext()) { var attribute = dataContext.Cards .Where(c => c.Id == cardId) .SelectMany(c => c.Attributes) .Where(a => a.Key == attributeKey) .FirstOrDefault(); if(attribute == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound); var previousAttributeDisplayIndex = dataContext.Cards .Where(c => c.Id == previousCardId) .SelectMany(c => c.Attributes) .Where(a => a.Key == attributeKey) .Select(a => a.DisplayOrder) .DefaultIfEmpty(-1) .FirstOrDefault(); attribute.DisplayOrder = previousAttributeDisplayIndex + 1; foreach(var followingAttribute in dataContext.Attributes .Where(a => a.Key == attributeKey) .Where(a => a.DisplayOrder > previousAttributeDisplayIndex)) { followingAttribute.DisplayOrder += 1; } dataContext.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } }