public ActionResult AddMember(AddMemberViewModel model) { if (model.Group.CreatedBy != User.Identity.Name || !(bool)Session["isAdmin"]) { ViewBag.ErrorMessage = "Sorry, You can't add members in this group"; return View("~/Views/Shared/Error.cshtml"); } if(ModelState.IsValid) { foreach( var member in model.members) { GroupsUsers groupsUsers = new GroupsUsers() { User = member.user, Group = model.Group, }; db.GroupUserRelations.Add(groupsUsers); } } db.SaveChanges(); return View("ListMembers", new { id=model.Group.GroupId }); }
public ActionResult Create(GroupViewModel groupViewModel) { try { // TODO: Add insert logic here if (ModelState.IsValid) { Group group = new Group() { GroupName = groupViewModel.GroupName, //Password = groupViewModel.Password, //CreatedBy = groupViewModel.CreatedBy, //CreatedDate = groupViewModel.CreatedDate, Status = groupViewModel.Status, //ModifiedBy = groupViewModel.ModifiedBy, //ModifiedDate = groupViewModel.ModifiedDate, }; db.Groups.Add(group); //db.SaveChanges(); if (!(bool)Session["isAdmin"]) { GroupsUsers groupsUsers = new GroupsUsers() { GroupId = group.GroupId, UserId = (int)HttpContext.Session["UserId"], }; db.GroupUserRelations.Add(groupsUsers); } db.SaveChanges(); //IdentityManager im = new IdentityManager(); //ApplicationUser user = new ApplicationUser() { UserName = group.GroupName, }; //im.CreateUser(user, group.Password); return RedirectToAction("AddMember", new { id=group.GroupId}); } return View(groupViewModel); } catch { return View(); } }