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 Edit(Skill skill) { if (ModelState.IsValid) { // get the category list from the db only once. List<Category> cats = db.Categories.ToList(); //// initialize the Skill's Category List. We're adding any that are still selected anyway, and don't want ones that were deselected to stick around. //skill.Categories = new List<Category>(); //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); // // also add the skill to the category. gotta make sure the foreign keys are set properly. // cat.Skills.Add(skill); // db.Entry(cat).State = EntityState.Modified; // } //} db.Entry(skill).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(skill); }
public SkillView(Skill g) { this.SkillObj = g; CategoriesList = new Dictionary<string, bool>(); List<Category> currCats = g.Categories.ToList(); foreach (Category cat in new CTContext().Categories.ToList()) { CategoriesList[cat.Name] = (currCats.Contains(cat)); } }