public ActionResult Create(Plan plan, string[] participants) { ModelState.Remove("Participants"); if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); plan.Owner = db.AppUsers.All().SingleOrDefault(au => au.Id == userId); if (participants != null) { foreach (var pGuid in participants) { var participant = db.Participants.All().FirstOrDefault(p => p.Id == pGuid); if (participant != null) { plan.Participants.Add(participant); } } } db.Plans.Add(plan); db.SaveChanges(); return RedirectToAction("Details", new { id = plan.Id }); } return View(plan); }
//[ValidateAntiForgeryToken] public JsonResult CreatePlan([DataSourceRequest] DataSourceRequest request, PlanViewModel plan) { if (plan != null && ModelState.IsValid) { ApplicationUser currentUser = this.db.AppUsers.All().FirstOrDefault(x => x.Id == plan.OwnerId); Plan newPlan = new Plan() { Title = plan.Title, Description = plan.Description, Owner = currentUser, //Participants = plan.Participants }; plan.Id = newPlan.Id; plan.OwnerName = currentUser.UserName; this.db.Plans.Add(newPlan); this.db.SaveChanges(); } return Json(new[] {plan}.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); }
public ActionResult Edit(Plan plan) { if (ModelState.IsValid) { db.Plans.Update(plan); db.SaveChanges(); return RedirectToAction("Details", new { id = plan.Id }); } return View(plan); }