public ActionResult Create([Bind(Include="BookId, Title, Author, Price, ISBN, Location, StackLocation")] BookViewModel bookVM, int StackLocation)
        {

            if (ModelState.IsValid)
            {
                Stack stack = (from s in db.Stacks
                              where s.StackId == StackLocation
                                  select s).FirstOrDefault();
                if(stack == null)
                {
                    stack = new Stack() { Location = bookVM.Location };
                    db.Stacks.Add(stack);
                }

                Book book = new Book()
                {
                    Author = bookVM.Author,
                    BookID = bookVM.BookId,
                    ISBN = bookVM.ISBN,
                    Price = bookVM.Price,
                    Title = bookVM.Title,
                    StackID = stack.StackId
                };
                db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StackLocation = new SelectList(db.Stacks.OrderBy(s => s.Location), "StackID", "Location");
            return View(bookVM);
        }
Exemplo n.º 2
0
 public HomeController() 
 {
     Stack stack1 = new Stack {Location="A1" };
     Book mobyDick = new Book{Title="Moby Dick", Author="Herman Melville", Price=5.00M , ISBN="9780746062760"};
     Book readyPlayerOne = new Book{Title="Ready Player One", Author="Ernest Cline", Price=10.00M, ISBN="9781446493830"};
     stack1.Books.Add(mobyDick);
     stack1.Books.Add(readyPlayerOne);
     store.Stacks.Add(stack1);
 }