Ejemplo n.º 1
0
        // GET: ClientGroups/Create
        public async Task <IActionResult> Create(int id)
        {
            //When the user clicks "Add Group to Client", from the Group Index view, or Group Details view, then the user will be directed to the ClientGroups Create View, which has been created using a view model.
            var user = await GetCurrentUserAsync();

            List <Client> clients = await _context.Clients
                                    .Include(c => c.User).Where(c => c.UserId == user.Id)
                                    .ToListAsync();



            var group = await _context.Groups
                        .Include(c => c.User)
                        .Include(g => g.ClientGroups)
                        .FirstOrDefaultAsync(m => m.Id == id && m.User == user);

            //instantiate view model
            var viewModel = new CreateClientGroupViewModel()
            {
                //display a drop down list of clients where the user will be able to select a client. Once the client is selected, a ClientGroup will be created, which will have the id of that specific client, and the id of the specific group.
                ClientNameOptions = clients.Select(c => new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text  = c.FullName
                }).ToList(),
                ClientGroup = new ClientGroup()
                {
                    GroupId = id
                }
            };

            ViewData["GroupId"] = group.GroupName;
            return(View(viewModel));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(CreateClientGroupViewModel vm)
        {
            if (ModelState.IsValid)
            {//if the model state is valid, the new ClientGroup will be added to the database
                _context.Add(vm.ClientGroup);

                await _context.SaveChangesAsync();

                //Direct the user to the Client Details page of the client who was chosen in the drop down box.
                return(RedirectToAction("Details", "Clients", new { id = vm.ClientGroup.ClientId }));
            }

            // If the post fails, rebuild the view model and send it back to the view
            List <Client> clients = await _context.Clients.ToListAsync();

            //display a drop down list of clients where the user will be able to select a client. Once the client is selected, a ClientGroup will be created, which will have the id of that specific client, and the id of the specific group.
            vm.ClientNameOptions = clients.Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text  = c.FullName
            }).ToList();

            return(View(vm));
        }