示例#1
0
        public ActionResult ViewSingleBook(int id)
        {
            Book   targetBook  = null;
            string authorNames = null;

            using (GaryBookstoreEntities dbc = new GaryBookstoreEntities())
            {
                targetBook = dbc.Books.Include("Inventory").SingleOrDefault(b => b.BookId == id);
                List <BookAuthor> authors = dbc.BookAuthors.Where(b => b.BookId == id).ToList();
                for (int i = 0; i < authors.Count; i++)
                {
                    BookAuthor author = authors[i];
                    authorNames += dbc.Authors.SingleOrDefault(a => a.Id == author.AuthorId).Name;
                    if (i < authors.Count() - 1)
                    {
                        authorNames += ", ";
                    }
                }
            }
            if (targetBook == null)
            {
                return(View("Error"));
            }
            else
            {
                ViewData["authors"] = authorNames;
                return(View("BookDetails", targetBook));
            }
        }
示例#2
0
 public ActionResult Index()
 {
     List<Book> featuredBooks = new List<Book>();
     List<int> ids = new List<int>(new int[] { 96, 110, 515 });
     using (GaryBookstoreEntities dbc = new GaryBookstoreEntities())
     {
         foreach (var id in ids)
         {
             Book book = dbc.Books.SingleOrDefault(b => b.BookId == id);
             featuredBooks.Add(book);
         }
     }
     return View(featuredBooks);
 }
示例#3
0
        public ActionResult ViewBooks(int pageNumber)
        {
            List <Book> allBooks = null;
            int         size     = 0;

            using (GaryBookstoreEntities dbc = new GaryBookstoreEntities())
            {
                size = dbc.Books.Count();
                if (size <= (pageNumber - 1) * bookPerPage)
                {
                    return(View("Error"));
                }
                int max = Math.Min(size, pageNumber * bookPerPage);
                allBooks = dbc.Books.Include("Inventory").Where(b => b.BookId >= (pageNumber - 1) * bookPerPage + 1 &&
                                                                b.BookId <= max).ToList();
            }
            ViewData["maxPage"] = (size - 1) / bookPerPage + 1;
            ViewData["curPage"] = pageNumber;
            return(View("BookList", allBooks));
        }