コード例 #1
0
        public async Task <IActionResult> Create([Bind("LoanId,LoanDate,UserId,CdId")] Loan loan)
        {
            if (ModelState.IsValid)
            {
                _context.Add(loan);
                await _context.SaveChangesAsync();

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

            List <SelectListItem> cdLoanSelectlist = new List <SelectListItem>();
            var allCDs = _context.Cd.ToList();

            for (var i = 0; i < allCDs.Count; ++i)
            {
                var loanExists = _context.Loan.Where(l => l.CdId == allCDs[i].Id).ToList();
                if (loanExists.Count == 0)
                {
                    var cdWithID = _context.Cd.Single(cd => cd.Id == allCDs[i].Id);
                    cdLoanSelectlist.Add(new SelectListItem {
                        Text = cdWithID.Title, Value = cdWithID.Id.ToString(), Disabled = false
                    });
                }
            }

            ViewData["CdId"]   = cdLoanSelectlist;
            ViewData["UserId"] = new SelectList(_context.User, "UserId", "FullName", loan.UserId);
            return(View(loan));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("UserId,FullName,Email,PhoneNr")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Id,Name,Cd_Id,Date,IsReturned")] Loan loan)
        {
            if (ModelState.IsValid)
            {
                _context.Add(loan);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(loan));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("ArtistId,Name")] Artist artist)
        {
            if (ModelState.IsValid)
            {
                _context.Add(artist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(artist));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("Id,Name,Artist_Id,Year")] Cd cd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(cd));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("Id,Title,Year,ArtistId")] Cd cd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ArtistId"] = new SelectList(_context.Artists, "ArtistId", "ArtistId", cd.ArtistId);
            return(View(cd));
        }
コード例 #7
0
        public async Task <IActionResult> Create([Bind("Id,ArtistId,Title,Genre,ReleaseYear,NoOfSongs,PlayTime,NoOfCds")] Cd cd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ArtistId"] = new SelectList(_context.Artist, "Id", "Name", cd.ArtistId);
            return(View(cd));
        }
コード例 #8
0
        public async Task <IActionResult> Create([Bind("TrackId,Name,cdId")] Track track)
        {
            if (ModelState.IsValid)
            {
                _context.Add(track);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["cdId"] = new SelectList(_context.Cds, "CdId", "CdId", track.cdId);
            return(View(track));
        }
コード例 #9
0
        public async Task <IActionResult> Create([Bind("RentId,Name,RentDate,cdId")] Rent rent)
        {
            if (ModelState.IsValid)
            {
                Cd result = (from p in _context.Cds
                             where p.CdId == rent.cdId
                             select p).SingleOrDefault();
                result.Avalable = false;

                _context.SaveChanges();

                _context.Add(rent);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.cdId = new SelectList(_context.Cds, "CdId", "Name");

            return(View(rent));
        }
コード例 #10
0
        public async Task <IActionResult> Create([Bind("Id,User,BorrowDate,CdId")] Borrow borrow)
        {
            if (ModelState.IsValid)
            {
                // Hämtar posten med samma ID som angivet
                var query =
                    from c in _context.Cd
                    where c.Id == borrow.CdId
                    select c;

                // Ändrar status till utlånad
                foreach (var c in query)
                {
                    c.Borrowed = true;
                }

                _context.Add(borrow);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CdId"] = new SelectList(_context.Cd, "Id", "Title", borrow.CdId);
            return(View(borrow));
        }