Beispiel #1
0
        // GET: Legends/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (_userManager.GetUserAsync(User).Result.IsAdmin)
            {
                if (id == null)
                {
                    return(NotFound());
                }
                var viewLocationList = _context.LegendViewLocation.Where(lv => lv.LegendId == id).Select(lv => lv.ViewLocationId).ToList();

                EditLegendLocationViewModel location = new EditLegendLocationViewModel()
                {
                    AvailableLocations = _context.ViewLocation.Include(l => l.User).ToList(),
                    Legend             = await _context.Legend.FindAsync(id),
                    LocationIds        = viewLocationList
                };



                if (location == null)
                {
                    return(NotFound());
                }
                ViewData["UserId"] = new SelectList(_context.ApplicationUser, "Id", "Id", location.Legend.UserId);
                return(View(location));
            }
            else
            {
                return(NotFound());
            }
        }
Beispiel #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));
        }
Beispiel #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());
            }
        }