示例#1
0
        public ActionResult Create([DataSourceRequest]DataSourceRequest request, BookViewModel book, 
            Dictionary<string, string> Category)
        {
            if (ModelState.IsValid)
            {
                int currCategoryId = int.Parse(Category["Id"].ToString());
                var entity = new Book
                {
                    Author = book.Author,
                    Description = book.Description,
                    Category = db.Categories.Find(currCategoryId),
                    Isbn = book.Isbn,
                    Title = book.Title
                };

                db.Books.Add(entity);
                db.SaveChanges();

                book.Author = entity.Author;
                book.Title = entity.Title;
                book.Description = entity.Description;
                book.Isbn = entity.Isbn;
                book.CategoryName = entity.Category.Name;
            }

            return Json(new[] { book }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
        private void CreateEditEntity(BookViewModel model, Book entity)
        {
            if (model.Category == null)
            {
                model.Category = Data.Categories.All().OrderBy(x => x.Id).Select(CategoryVewModel.ToViewModel).FirstOrDefault();
            }

            entity.Author = model.Author;
            if (model.Category != null)
            {
                model.CategoryName = model.Category.Name;
                var category = Data.Categories.GetById(model.Category.Id);
                if (category != null)
                {
                    entity.Category = category;
                }
                else
                {
                    entity.Category = new Category
                    {
                        Id = model.Category.Id,
                        Name = model.Category.Name
                    };
                }
            }
            entity.Description = model.Description;
            entity.Isbn = model.Isbn;
            entity.Title = model.Title;
            entity.WebSite = model.WebSite;

            this.Data.SaveChanges();
        }
        public JsonResult UpdateCommand([DataSourceRequest] DataSourceRequest request, BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                var entity = this.Data.Books.GetById(model.Id);
                if (entity != null)
                {
                    CreateEditEntity(model, entity);
                }
            }

            return Json(new[] { model }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
        public ActionResult CreateCommand([DataSourceRequest] DataSourceRequest request, BookViewModel model)
        {
            var entity = new Book();
            Data.Books.Add(entity);

            if (ModelState.IsValid)
            {
                CreateEditEntity(model, entity);

                model.Id = entity.Id;
                model.CategoryName = entity.Category.Name;
            }

            return Json(new[] { model }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
示例#5
0
 public ActionResult Create(BookViewModel model, int Category)
 {
     if (model != null)
     {
         var selectedCategory = db.Categories.Find(Category);
         var newBook = new Book();
         newBook.Author = model.Author;
         newBook.Description = model.Description;
         newBook.ISBN = model.ISBN;
         newBook.Title = model.Title;
         newBook.WebSite = model.WebSite;
         newBook.Category = selectedCategory;
         db.Books.Add(newBook);
         db.SaveChanges();
     }
     return Content("");
 }
示例#6
0
        public ActionResult Update([DataSourceRequest] DataSourceRequest request,
            BookViewModel product)
        {
            if (product != null && ModelState.IsValid)
            {
                var target = db.Books.Find(product.Id);
                var category = db.Categories.Find(product.Category.Id);
                if (target != null)
                {
                    target.Author = product.Author;
                    target.Description = product.Description;
                    target.Id = product.Id;
                    target.ISBN = product.ISBN;
                    target.Title = product.Title;
                    target.WebSite = product.WebSite;
                    target.Category = category;
                }
                db.SaveChanges();
            }

            return Json(new[]{product}.ToDataSourceResult(request, ModelState));
        }
示例#7
0
        public ActionResult Update([DataSourceRequest]DataSourceRequest request, BookViewModel book,
            Dictionary<string, string> Category)
        {
            if (ModelState.IsValid)
            {
                var selectedCategoryId = int.Parse(Category["Id"]);
                var currBook = db.Books.Find(book.Id);
                currBook.Title = book.Title;
                currBook.Author = book.Author;
                currBook.Description = book.Description;
                currBook.Isbn = book.Isbn;
                currBook.Category = db.Categories.Find(selectedCategoryId);

                db.SaveChanges();

                book.Title = currBook.Title;
                book.Description = currBook.Description;
                book.Isbn = currBook.Isbn;
                book.Author = currBook.Author;
                book.CategoryName = currBook.Category.Name;
            }

            return Json(new[] { book }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
示例#8
0
        public ActionResult Destroy([DataSourceRequest]DataSourceRequest request, BookViewModel book)
        {
            if (ModelState.IsValid)
            {
                var currBook = db.Books.Find(book.Id);
                db.Books.Remove(currBook);
                db.SaveChanges();
            }

            return Json(new[] { book }.ToDataSourceResult(request, ModelState));
        }