public ActionResult Create(Skill skill) { if (ModelState.IsValid) { List<Category> cats = db.Categories.ToList(); // get the category list from the db only once. //skill.Categories = new List<Category>(); // initialize the Skill's Category List, since it starts null. //foreach (string k in skill.CategoriesList.Keys) //{ // // if the user selected this key name // if (skill.CategoriesList[k]) { // Category cat = cats.First(c => c.Name == k); // // add the category matching the key name // skill.Categories.Add(cat); // cat.Skills.Add(skill); // db.Entry(cat).State = EntityState.Modified; // } //} UserManager manager = new UserManager(db); skill.User = manager.findByUserName(User.Identity.Name); db.Skills.Add(skill); db.SaveChanges(); return RedirectToAction("Index"); } return View(skill); }
public ActionResult Create(Goal goal) { if (ModelState.IsValid) { int checkYear = 1900; int inYear = goal.DueDate.Year; if (inYear < checkYear) { ViewBag.DateValidation = "Please enter a date between 1900 and now."; return View(); } UserManager manager = new UserManager(db); goal.User = manager.findByUserName(User.Identity.Name); db.Goals.Add(goal); db.SaveChanges(); return RedirectToAction("Edit", new { id = goal.ID }); } return View(goal); }