public ActionResult Create(Task task) { if (ModelState.IsValid) { task.ID = Guid.NewGuid(); context.Tasks.Add(task); context.SaveChanges(); return RedirectToAction("Index"); } ViewBag.PossibleTaskLists = context.TaskLists; ViewBag.PossiblePriorities = context.Priorities; return View(task); }
public ActionResult Edit(Task task) { if (ModelState.IsValid) { context.Entry(task).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("Index"); } ViewBag.PossibleTaskLists = context.TaskLists; ViewBag.PossiblePriorities = context.Priorities; return View(task); }
private bool Update(Task requestedTask, Task originalTask, Task newTask) { bool updated = false; // timestamps!! Type t = requestedTask.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { object serverValue = pi.GetValue(requestedTask, null); object origValue = pi.GetValue(originalTask, null); object newValue = pi.GetValue(newTask, null); // if this is the TasgTags field make it simple - if this update is the last one, it wins if (pi.Name == "TaskTags") { if (newTask.LastModified > requestedTask.LastModified) { pi.SetValue(requestedTask, newValue, null); updated = true; } continue; } // if the value has changed, process further if (!Object.Equals(origValue, newValue)) { // if the server has the original value, or the new task has a later timestamp than the server, then make the update if (Object.Equals(serverValue, origValue) || newTask.LastModified > requestedTask.LastModified) { pi.SetValue(requestedTask, newValue, null); updated = true; } } } return updated; }