示例#1
0
        public async Task <IActionResult> Edit(int id, [Bind("AuthorId,FirstName,LastName")] Author author)
        {
            if (id != author.AuthorId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var context = new EFCoreWebDemoContext())
                    {
                        context.Update(author);
                        await context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(author.AuthorId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            //return View(author);
            return(RedirectToAction("Index"));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("Title, AuthorId")] Book book)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                context.Books.Add(book);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("FirstName, LastName")] Author author)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                context.Add(author);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
示例#4
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var _context = new EFCoreWebDemoContext())
            {
                var book = await _context.Books.FindAsync(id);

                _context.Books.Remove(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
示例#5
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var author = await context.Authors.FindAsync(id);

                context.Authors.Remove(author);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
示例#6
0
        public async Task <IActionResult> Create([Bind("Title, AuthorId")] Book book)
        {
            if (ModelState.IsValid)
            {
                using (var _context = new EFCoreWebDemoContext())
                {
                    _context.Add(book);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", book.AuthorId);
            }
            return(View(book));
            //using (var context = new EFCoreWebDemoContext())
            //{
            //    context.Books.Add(book);
            //    await context.SaveChangesAsync();
            //    return RedirectToAction("Index");
            //}
        }
示例#7
0
        public async Task <IActionResult> Edit(int id, [Bind("BookId,Title,AuthorId")] Book book)
        {
            if (id != book.BookId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var _context = new EFCoreWebDemoContext())
                    {
                        _context.Update(book);
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookExists(book.BookId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", book.AuthorId);
            }
            return(View(book));
        }