AuthorsControllers.cs
AuthorsController.cs
as follows:
|
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using BookStore3.Data; using BookStore3.Models; namespace BookStore3.Controllers { public class AuthorsController : Controller { private readonly BookStore3Context _context; public AuthorsController( BookStore3Context context ) { _context = context; } // GET: Authors public async Task<IActionResult> Index( ) { var bookStore3Context = _context.Author.Include( a => a.Book ); return View( await bookStore3Context.ToListAsync( ) ); } // GET: Authors/Details/5 public async Task<IActionResult> Details( int? id ) { if ( id == null ) { return NotFound( ); } var author = await _context.Author .Include( a => a.Book ) .FirstOrDefaultAsync( m => m.Id == id ); if ( author == null ) { return NotFound( ); } return View( author ); } // GET: Authors/Create public IActionResult Create( ) { ViewData["BookId"] = new SelectList( _context.Book, "Id", "Title" ); return View( ); } // POST: Authors/Create // To protect from overposting attacks, // enable the specific properties you want to bind to. // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [ HttpPost ] [ ValidateAntiForgeryToken ] public async Task<IActionResult> Create( [ Bind( "Id,Name, BookId" ) ] Author author ) { if ( ModelState.IsValid ) { _context.Add( author ); await _context.SaveChangesAsync( ); return RedirectToAction( nameof( Index ) ); } ViewData["BookId"] = new SelectList( _context.Book, "Id", "Title", author.BookId ); return View( author ); } // GET: Authors/Edit/5 public async Task<IActionResult> Edit( int? id ) { if ( id == null ) { return NotFound( ); } var author = await _context.Author.FindAsync( id ); if ( author == null ) { return NotFound( ); } ViewData["BookId"] = new SelectList( _context.Book, "Id", "Title", author.BookId ); return View( author ); } // POST: Authors/Edit/5 // To protect from overposting attacks, // enable the specific properties you want to bind to. // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [ HttpPost ] [ ValidateAntiForgeryToken ] public async Task<IActionResult> Edit( int id, [ Bind( "Id, Name, BookId " ) ] Author author ) { if ( id != author.Id ) { return NotFound( ); } if ( ModelState.IsValid ) { try { _context.Update( author ); await _context.SaveChangesAsync( ); } catch ( DbUpdateConcurrencyException ) { if ( !AuthorExists( author.Id ) ) { return NotFound( ); } else { throw; } } return RedirectToAction( nameof( Index ) ); } ViewData["BookId"] = new SelectList( _context.Book, "Id", "Title", author.BookId); return View( author ); } // GET: Authors/Delete/5 public async Task<IActionResult> Delete( int? id ) { if ( id == null ) { return NotFound( ); } var author = await _context.Author .Include( a => a.Book ) .FirstOrDefaultAsync( m => m.Id == id ); if ( author == null ) { return NotFound( ); } return View( author ); } // POST: Authors/Delete/5 [ HttpPost, ActionName( "Delete" ) ] [ ValidateAntiForgeryToken ] public async Task<IActionResult> DeleteConfirmed( int id ) { var author = await _context.Author.FindAsync( id ); if ( author != null ) { _context.Author.Remove( author ); } await _context.SaveChangesAsync( ); return RedirectToAction( nameof( Index ) ); } private bool AuthorExists( int id ) { return _context.Author.Any( e => e.Id == id ); } } } |
“According to most studies, people’s number one fear is public speaking. Number two is death. Death is number two. Does that sound right? This means to the average person, if you go to a funeral, you’re better off in the casket than doing the eulogy.” — Jerry Seinfeld |