コード例 #1
0
        public ActionResult Details(int?id)
        {
            if (id != null)
            {
                ContactRepository contactRepo = new ContactRepository();

                Contact contact = contactRepo.GetById(id.Value);

                if (contact != null && contact.UserID == AuthenticationService.LoggedUser.ID)
                {
                    ContactCreateEditVM model = new ContactCreateEditVM()
                    {
                        ID        = contact.ID,
                        FirstName = contact.FirstName,
                        LastName  = contact.LastName,
                        Email     = contact.Email
                    };

                    GroupRepository groupRepo = new GroupRepository();

                    List <Group> contactGroups = contactRepo.GetAll().Where(c => c.ID == id).First().Groups;

                    if (contactGroups.Count > 0)
                    {
                        List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>();

                        foreach (Group group in contactGroups)
                        {
                            checkBoxListItems.Add(new CheckBoxListItem()
                            {
                                Text = group.Name
                            });
                        }

                        model.Groups = checkBoxListItems;
                    }

                    return(View(model));
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public ActionResult CreateEdit(ContactCreateEditVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Contact contact;

            ContactRepository contactRepo = new ContactRepository();

            if (model.ID > 0) // Edit
            {
                contact = contactRepo.GetById(model.ID);

                if (contact == null || contact.UserID != model.UserID)
                {
                    return(HttpNotFound());
                }
            }

            else // Create
            {
                contact = new Contact()
                {
                    UserID = model.UserID,
                    Groups = new List <Group>()
                };
            }

            if (contact.UserID == AuthenticationService.LoggedUser.ID)
            {
                if (model.GroupIds != null)
                {
                    GroupRepository groupRepo = new GroupRepository();
                    groupRepo.Context = contactRepo.Context;
                    groupRepo.DbSet   = groupRepo.Context.Set <Group>();

                    if (contact.Groups != null)
                    {
                        contact.Groups.Clear();
                    }

                    foreach (int groupId in model.GroupIds)
                    {
                        Group group = groupRepo.GetById(groupId);
                        contact.Groups.Add(group);
                    }
                }

                else
                {
                    contact.Groups.Clear();
                }

                contact.FirstName = model.FirstName;
                contact.LastName  = model.LastName;
                contact.Email     = model.Email;

                contactRepo.Save(contact);

                return(RedirectToAction("Index"));
            }

            return(HttpNotFound());
        }
コード例 #3
0
        public ActionResult CreateEdit(int?id)
        {
            if (id == null) // Create
            {
                ContactCreateEditVM model = new ContactCreateEditVM()
                {
                    UserID = AuthenticationService.LoggedUser.ID
                };

                GroupRepository groupRepo = new GroupRepository();

                List <Group> allGroups = groupRepo.GetAll(g => g.UserID == AuthenticationService.LoggedUser.ID);

                List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>();

                foreach (Group group in allGroups)
                {
                    checkBoxListItems.Add(new CheckBoxListItem()
                    {
                        ID        = group.ID,
                        Text      = group.Name,
                        IsChecked = false
                    });
                }

                model.Groups = checkBoxListItems;

                return(View(model));
            }

            if (id > 0) // Edit
            {
                ContactRepository contactRepo = new ContactRepository();

                Contact contact = contactRepo.GetById(id.Value);

                if (contact != null && contact.UserID == AuthenticationService.LoggedUser.ID)
                {
                    ContactCreateEditVM model = new ContactCreateEditVM()
                    {
                        ID        = contact.ID,
                        UserID    = contact.UserID,
                        FirstName = contact.FirstName,
                        LastName  = contact.LastName,
                        Email     = contact.Email
                    };

                    GroupRepository groupRepo = new GroupRepository();

                    List <Group> allGroups = groupRepo.GetAll(g => g.UserID == AuthenticationService.LoggedUser.ID);

                    //List<Group> contactGroups = groupRepo.GetAll(g => g.Contacts.Contains(contact));
                    List <Group> contactGroups = contactRepo.GetAll().Where(c => c.ID == id).First().Groups;

                    List <CheckBoxListItem> checkBoxListItems = new List <CheckBoxListItem>();

                    foreach (Group group in allGroups)
                    {
                        checkBoxListItems.Add(new CheckBoxListItem()
                        {
                            ID   = group.ID,
                            Text = group.Name,
                            //We should have already-selected genres be checked
                            IsChecked = contactGroups.Where(g => g.ID == group.ID).Any()
                        });
                    }

                    model.Groups = checkBoxListItems;

                    return(View(model));
                }
            }

            return(RedirectToAction("Index"));
        }