public ActionResult Add() { ViewBag.InstitutionID = new SelectList(db.Institutions, "ID", "Name"); var viewmodel = new AddTeamVM(); viewmodel.Categories = new List<AddTeamVM.Category>(); foreach (var category in db.Categories) { var cat = new AddTeamVM.Category() { CategoryID = category.ID, CategoryName = category.Name, isChecked = false }; viewmodel.Categories.Add(cat); } return View(viewmodel); }
public ActionResult Add(AddTeamVM model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.Email, PasswordHash = (new PasswordHasher()).HashPassword(model.Password)}; var selectedCategories = new List<Category>(); foreach (var category in model.Categories) { if (category.isChecked) { selectedCategories.Add(db.Categories.Where(c => c.ID == category.CategoryID).First()); } } var team = new Team() { Name = model.Name, InstitutionID = model.InstitutionID, ApplicationUserID = user.Id, Categories = selectedCategories }; var result = usermanager.Create(user); if (result.Succeeded) { usermanager.AddToRole(user.Id, "Team"); db.Teams.Add(team); db.SaveChanges(); return RedirectToAction("Index"); } } ViewBag.InstitutionID = new SelectList(db.Institutions, "ID", "Name"); model.Categories = new List<AddTeamVM.Category>(); foreach (var category in db.Categories) { var cat = new AddTeamVM.Category() { CategoryID = category.ID, CategoryName = category.Name, isChecked = false }; model.Categories.Add(cat); } return View(model); }