public async Task <IActionResult> Create(LocationsLegendViewModel viewModel, List <int> ViewLocationInput)
        {
            ModelState.Remove("ViewLocation.UserId");
            ModelState.Remove("ViewLocation.User");
            if (ModelState.IsValid)
            {
                var location    = viewModel.ViewLocation;
                var currentUser = await _userManager.GetUserAsync(HttpContext.User);

                viewModel.ViewLocation.User   = currentUser;
                viewModel.ViewLocation.UserId = currentUser.Id;
                _context.Add(location);
                foreach (var id in ViewLocationInput)
                {
                    LegendViewLocation newView = new LegendViewLocation()
                    {
                        ViewLocationId = location.ViewLocationId,
                    };
                }
                await _context.SaveChangesAsync();

                if (_userManager.GetUserAsync(User).Result.IsAdmin)
                {
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    return(RedirectToAction(nameof(LegendsIndex)));
                }
            }
            ViewData["UserId"] = new SelectList(_context.ApplicationUser, "Id", "Id", viewModel.ViewLocation.UserId);
            return(View(viewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(EditLegendLocationViewModel viewModel, List <int> ViewLocationInput)
        {
            ModelState.Remove("Legend.UserId");
            ModelState.Remove("ViewLocationInput");
            ModelState.Remove("Legend.LegendViewLocations");
            if (ModelState.IsValid)
            {
                var legend = viewModel.Legend;
                //var legendViewLocations = _context.LegendViewLocation.Include(l => l.Legend.Title).Include(l=> l.ViewLocation.Name).ToList();
                //var legendViewName = legendViewLocations.Select(l => l.ViewLocation.Name);
                //var legendTitle = legendViewLocations.Select(l => l.Legend.Title);
                var currentUser = await _userManager.GetUserAsync(HttpContext.User);

                legend.UserId         = currentUser.Id;
                viewModel.LocationIds = ViewLocationInput;
                _context.Add(legend);

                foreach (var id in ViewLocationInput)
                {
                    LegendViewLocation newView = new LegendViewLocation()
                    {
                        LegendId       = legend.LegendId,
                        ViewLocationId = id,
                    };
                    _context.Add(newView);
                }


                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(viewModel));
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(int id, EditLegendLocationViewModel viewModel, List <int> ViewLocationInput)
        {
            if (_userManager.GetUserAsync(User).Result.IsAdmin)
            {
                if (id != viewModel.Legend.LegendId)
                {
                    return(NotFound());
                }
                ModelState.Remove("User");
                ModelState.Remove("Legend.LegendViewLocations");
                if (ModelState.IsValid)
                {
                    List <LegendViewLocation> viewLocations = await _context.LegendViewLocation.Where(lv => lv.LegendId == id).ToListAsync();

                    viewLocations.ForEach(lv => _context.LegendViewLocation.Remove(lv));
                    try
                    {
                        var legend = viewModel.Legend;
                        viewModel.LocationIds = ViewLocationInput;
                        _context.Legend.Update(legend);

                        foreach (var item in ViewLocationInput)
                        {
                            LegendViewLocation newView = new LegendViewLocation()
                            {
                                LegendId       = legend.LegendId,
                                ViewLocationId = item,
                            };

                            _context.Add(newView);
                        }
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!LegendExists(viewModel.Legend.LegendId))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction(nameof(PendingIndex)));
                }
                ViewData["UserId"] = new SelectList(_context.ApplicationUser, "Id", "Id", viewModel.Legend.UserId);
                return(View(viewModel));
            }
            else
            {
                return(NotFound());
            }
        }