コード例 #1
0
        public async Task <IActionResult> Create([Bind("CompositionId,Coordinates,CompPlaceId")] CompPlace compPlace)
        {
            string session   = HttpContext.Session.GetString("user");
            int    accountId = 0;

            if (session != null)
            {
                accountId = Convert.ToInt32(JsonConvert.DeserializeObject(session));
            }

            if (ModelState.IsValid)
            {
                _context.Add(compPlace);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CompositionId"] = new SelectList(_context.Composition.Where(c => c.AccountId == accountId), "CompositionId", "Name", compPlace.CompositionId);
            ViewData["Coordinates"]   = new SelectList(_context.Place, "Coordinates", "Name", compPlace.Coordinates);
            return(View(compPlace));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("CompositionId,Coordinates,CompPlaceId")] CompPlace compPlace)
        {
            if (id != compPlace.CompPlaceId)
            {
                return(NotFound());
            }

            string session   = HttpContext.Session.GetString("user");
            int    accountId = 0;

            if (session != null)
            {
                accountId = Convert.ToInt32(JsonConvert.DeserializeObject(session));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(compPlace);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CompPlaceExists(compPlace.CompPlaceId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CompositionId"] = new SelectList(_context.Composition.Where(c => c.AccountId == accountId), "CompositionId", "Name", compPlace.CompositionId);
            ViewData["Coordinates"]   = new SelectList(_context.Place, "Coordinates", "Name", compPlace.Coordinates);
            return(View(compPlace));
        }
コード例 #3
0
        public IActionResult CreateAll(VMCompFishPlaceLure viewModel)
        {
            string session = HttpContext.Session.GetString("user");

            if (session != null)
            {
                int accountId = Convert.ToInt32(JsonConvert.DeserializeObject(session));
                var account   = _context.Account.FindAsync(accountId);
                var name      = account != null ? account.Result.Mail : "not logged IN!!!";

                viewModel.Composition.AccountId = accountId;
                ViewData["AccountId"]           = name;
            }
            else
            {
                ViewData["AccountId"] = "Not logged in";
                return(RedirectToAction("index", "Home"));
            }



            if (ModelState.IsValid)
            {
                _context.Add(viewModel.Composition);
                _context.SaveChanges();

                if (viewModel.Fishes != null)
                {
                    foreach (var fish in viewModel.Fishes)
                    {
                        CompFish cf = new CompFish
                        {
                            Composition = viewModel.Composition,
                            FishId      = fish
                        };

                        _context.Add(cf);
                    }
                }

                if (viewModel.Lures != null)
                {
                    foreach (var lure in viewModel.Lures)
                    {
                        CompLure cl = new CompLure
                        {
                            Composition = viewModel.Composition,
                            LureId      = lure
                        };

                        _context.Add(cl);
                    }
                }

                if (viewModel.Places != null)
                {
                    foreach (var place in viewModel.Places)
                    {
                        CompPlace cp = new CompPlace
                        {
                            Composition = viewModel.Composition,
                            Coordinates = place
                        };

                        _context.Add(cp);
                    }
                }

                _context.SaveChanges();
                HttpContext.Session.SetString("added", "true");

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

            ViewData["ReelId"]      = new SelectList(_context.Reel, "ReelId", "Brand");
            ViewData["RodId"]       = new SelectList(_context.Rod, "RodId", "Brand");
            ViewData["FishId"]      = new SelectList(_context.Fish, "FishId", "Species");
            ViewData["LureId"]      = new SelectList(_context.Lure, "LureId", "Brand");
            ViewData["Coordinates"] = new SelectList(_context.Place, "Coordinates", "Name");

            return(View());
        }
コード例 #4
0
        public async Task <IActionResult> EditAll(int id, VMCompFishPlaceLure viewModel)
        {
            if (id != viewModel.Composition.CompositionId)
            {
                return(NotFound());
            }

            string session = HttpContext.Session.GetString("user");

            if (session != null)
            {
                ViewData["AccountId"] = session;
                int accountId = Convert.ToInt32(JsonConvert.DeserializeObject(session)); // denna får inte vara null
                viewModel.Composition.AccountId = accountId;
            }
            else
            {
                ViewData["AccountId"] = "Log in to edit compositions";
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewModel.Composition);
                    await _context.SaveChangesAsync();

                    if (viewModel.Fishes != null)
                    {
                        var compFishes = from matches in _context.CompFish
                                         where matches.CompositionId == id
                                         select matches;


                        _context.CompFish.RemoveRange(compFishes);

                        foreach (var fish in viewModel.Fishes)
                        {
                            CompFish cf = new CompFish
                            {
                                Composition = viewModel.Composition,
                                FishId      = fish
                            };

                            _context.Add(cf);
                        }
                    }

                    if (viewModel.Lures != null)
                    {
                        var compLures = from matches in _context.CompLure
                                        where matches.CompositionId == id
                                        select matches;

                        _context.CompLure.RemoveRange(compLures);

                        foreach (var lure in viewModel.Lures)
                        {
                            CompLure cl = new CompLure
                            {
                                Composition = viewModel.Composition,
                                LureId      = lure
                            };

                            _context.Add(cl);
                        }
                    }

                    if (viewModel.Places != null)
                    {
                        var compPlaces = from matches in _context.CompPlace
                                         where matches.CompositionId == id
                                         select matches;

                        _context.CompPlace.RemoveRange(compPlaces);

                        foreach (var place in viewModel.Places)
                        {
                            CompPlace cp = new CompPlace
                            {
                                Composition = viewModel.Composition,
                                Coordinates = place
                            };

                            _context.Add(cp);
                        }
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CompositionExists(viewModel.Composition.CompositionId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            ViewData["ReelId"]      = new SelectList(_context.Reel, "ReelId", "Brand", viewModel.Composition.ReelId);
            ViewData["RodId"]       = new SelectList(_context.Rod, "RodId", "Brand", viewModel.Composition.RodId);
            ViewData["FishId"]      = new SelectList(_context.Fish, "FishId", "Species");
            ViewData["LureId"]      = new SelectList(_context.Lure, "LureId", "Brand");
            ViewData["Coordinates"] = new SelectList(_context.Place, "Coordinates", "Name");
            return(View(viewModel.Composition));
        }