コード例 #1
0
        public ActionResult _InsertUserGroup(string userID,  GroupLookupViewModel viewGroup)
        {
            int userIntID;
            try
            {
                userIntID = Convert.ToInt32(userID);
            }
            catch (Exception)
            {
                return ErrMsgView("Sorry - An invalid User ID was used to insert a group.");
            }
            if (ModelState.IsValid)
            {
                var addGroup = (from g in PrdnDBContext.Groups
                                where g.ID == viewGroup.ID
                                select g).FirstOrDefault();

                if (addGroup != null)
                {
                    var user = (from u in PrdnDBContext.Users
                                where u.ID == userIntID
                                select u).FirstOrDefault();

                    if (user != null)
                    {
                        user.Groups.Add(addGroup);
                        PrdnDBContext.SaveChanges();
                    }
                    else
                    {
                        ModelState.AddModelError(viewGroup.FullPropertyName(m => m.AppID), "invalid user ID");
                    }
                }
                else
                {
                    ModelState.AddModelError(viewGroup.FullPropertyName(m => m.ID), "invalid group ID");
                }
            }
            return View(UserGroupGridList(userIntID));
        }
コード例 #2
0
        public ActionResult _DeleteUserGroup(string userID, GroupLookupViewModel viewGroup)
        {
            int userIntID;
            try
            {
                userIntID = Convert.ToInt32(userID);
            }
            catch (Exception)
            {
                return ErrMsgView("Sorry - An invalid User ID was used to insert a group.");
            }
            if (ModelState.IsValid)
            {
                User usr = (from u in PrdnDBContext.Users
                            where u.ID == userIntID
                            select u).FirstOrDefault();

                if (usr != null)
                {
                    Group grp = (from u in usr.Groups
                                 where u.ID == viewGroup.ID
                                 select u).FirstOrDefault();

                    if (grp != null)
                    {
                        usr.Groups.Remove(grp);
                        PrdnDBContext.SaveChanges();
                    }
                    else
                    {
                        ModelState.AddModelError("ID", "Group ID " + viewGroup.ID.ToString() + " was not a member of group " + grp.Code + ".");
                    }
                }
                else
                {
                    ModelState.AddModelError("ID", "invalid user ID");
                }
            }
            return View(UserGroupGridList(userIntID));
        }