Ejemplo n.º 1
0
        public async Task <IActionResult> Create(CreateConversationTypeViewModel createConversationType)
        {
            if (ModelState.IsValid)
            {
                var conversationType = new ConversationType
                {
                    ConversationName     = createConversationType.Name,
                    ConversationDuration = createConversationType.Duration,
                    School = _context.Schools.First(s => s.Id == Guid.Parse(HttpContext.Session.GetString("School")))
                };

                _context.Add(conversationType);
                foreach (var group in createConversationType.SelectedGroups)
                {
                    ConversationTypeClaim conversationTypeClaim = new ConversationTypeClaim
                    {
                        ConversationType = conversationType,
                        Group            = _context.ApplicationUserGroups.First(g => g.Id == group)
                    };
                    _context.Add(conversationTypeClaim);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            createConversationType.Groups = new SelectList(_context.ApplicationUserGroups.OrderBy(g => g.GroupName).ToList(),
                                                           "Id", "GroupName");
            return(View(createConversationType));
        }
        public async Task <IActionResult> Create(CreateConversationTypeViewModel createConversationType)
        {
            if (ModelState.IsValid)
            {
                var conversationType = new ConversationType
                {
                    ConversationName     = createConversationType.Name,
                    ConversationDuration = createConversationType.Duration
                };
                if (User.IsInRole("Eigenaar") && HttpContext.Session.GetString("School") == null)
                {
                    conversationType.School =
                        _context.Schools.First(s => s.Id == Guid.Parse(createConversationType.SelectedSchool));
                }
                else
                {
                    conversationType.School =
                        _context.Schools.First(s => s.Id == Guid.Parse(HttpContext.Session.GetString("School")));
                }
                _context.Add(conversationType);
                foreach (var group in createConversationType.SelectedGroups)
                {
                    ConversationTypeClaim conversationTypeClaim = new ConversationTypeClaim
                    {
                        ConversationType = conversationType,
                        Group            = _context.ApplicationUserGroups.First(g => g.Id == group)
                    };
                    _context.Add(conversationTypeClaim);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            createConversationType.Groups = new List <SelectListItem>();
            var groups = _context.ApplicationUserGroups.Include(g => g.School).OrderBy(g => g.GroupName).GroupBy(g => g.School).ToList();

            foreach (var groupList in groups)
            {
                var selectGroup = new SelectListGroup {
                    Name = groupList.Key.Name, Disabled = false
                };
                foreach (var group in groupList)
                {
                    ((List <SelectListItem>)createConversationType.Groups).Add(new SelectListItem {
                        Disabled = false, Group = selectGroup, Value = group.Id.ToString(), Text = group.GroupName
                    });
                }
                //((List<SelectListItem>)conversationTypeModel.Groups).Add(new SelectListItem { Text = group.GroupName, Value = group.Id.ToString(), Selected = true });
            }
            createConversationType.Schools = new SelectList(_context.Schools.ToList(), "Id", "Name");

            return(View(createConversationType));
        }