public ActionResult Create([DataSourceRequest] DataSourceRequest request, BookAdministrationViewModel book) { if (book != null && ModelState.IsValid) { var category = db.Categories.Find(book.Category.Id); if (category == null || book.Category.Id == 0) { category = db.Categories.First(); } var newBook = new Book() { Title = book.Title, Author = book.Author, ISBN = book.ISBN, Website = book.Website, Description = book.Description, Category = category }; db.Books.Add(newBook); db.SaveChanges(); book.Category.Name = newBook.Category.Name; } return Json(new[] { book }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); }
protected void LinkButtonCreate_OnClick(object sender, EventArgs e) { this.Page.Validate("ValidateAddBookGroup"); if (Page.IsValid) { string title = this.TextBoxAddTitle.Text; string author = this.TextBoxAddAuthor.Text; string isbn = this.TextBoxAddISBN.Text; string website = this.TextBoxAddWebsite.Text; string description = this.TextBoxAddDescription.Text; var context = new ApplicationDbContext(); var selectedCategory = this.DropDownListAddCategory.SelectedItem.Text; var selectedCategoryEntity = context.Categories.FirstOrDefault(c => c.Name == selectedCategory); var book = new Book() { Title = title, Author = author, ISBN = isbn, Website = website, Description = description, Category = selectedCategoryEntity }; context.Books.Add(book); context.SaveChanges(); this.PanelCreateBook.Visible = false; Response.Redirect("EditBooks.aspx"); } }
public JsonResult CreateBook([DataSourceRequest] DataSourceRequest request, BookViewModel book) { if (book != null && ModelState.IsValid) { int bookId = int.Parse(book.Category); var category = this.Data.Categories.FirstOrDefault(x => x.ID == bookId); var newBook = new Book { Title = book.Title, Description = book.Description, Author = book.Author, Category = category, ISBN = book.ISBN, WebSite = book.WebSite }; this.Data.Books.Add(newBook); this.Data.SaveChanges(); book.ID = newBook.ID; } 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 ActionResult BooksCreate([DataSourceRequest]DataSourceRequest request, BookViewModel book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Book newBook = new Book() { Description = book.Description, Author = book.Author, ISBN = book.ISBN, Title = book.Title, Website = book.Website }; var category = db.Categories.FirstOrDefault(x => x.Name == book.CategoryName); category = CreateOrGetCategory(book, db, newBook, category); newBook.Id = db.Books.Add(newBook).Id; db.SaveChanges(); book.Id = newBook.Id; } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }
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); }
public void FormViewIsertBook_InsertItem() { var item = new LibrarySystem.Models.Book(); TryUpdateModel(item); if (ModelState.IsValid) { this.dbContext.Books.Add(item); this.dbContext.SaveChanges(); } }
public void FormViewBook_InsertItem() { var item = new LibrarySystem.Models.Book(); TryUpdateModel(item); if (ModelState.IsValid) { this.db.Books.Add(item); this.db.SaveChanges(); } }
public ActionResult Create(Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", book.CategoryId); return View(book); }
protected void CreateBookButton_Command(object sender, CommandEventArgs e) { var context = new LibrarySystemEntities(); string title = this.TextBoxTitle.Text; string author = this.TextBoxAuthor.Text; string isbn = this.TextBoxISBN.Text; string webSite = this.TextBoxWebSite.Text; string description = this.TextBoxDescription.Text; int categoryIndex = int.Parse(this.DropDownListCategory.SelectedValue); if (string.IsNullOrWhiteSpace(title)) { ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty"); this.Response.Redirect("~/Admin/EditCategories.aspx"); } if (string.IsNullOrWhiteSpace(author)) { ErrorSuccessNotifier.AddErrorMessage("Author cannot be empty"); this.Response.Redirect("~/Admin/EditCategories.aspx"); } if (title.Length < 3 || title.Length > 256) { ErrorSuccessNotifier.AddErrorMessage("Title's length should be between 3 and 256 symbols"); this.Response.Redirect("~/Admin/EditBooks.aspx"); } if (author.Length < 3 || author.Length > 100) { ErrorSuccessNotifier.AddErrorMessage("Author's length should be between 3 and 100 symbols"); this.Response.Redirect("~/Admin/EditBooks.aspx"); } var category = context.Categories.FirstOrDefault(c => c.Id == categoryIndex); Book newBook = new Book(); newBook.Title = title; newBook.Author = author; newBook.ISBN = isbn; newBook.WebSite = webSite; newBook.Description = description; newBook.Category = category; category.Books.Add(newBook); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book created!"); this.Response.Redirect("~/Admin/EditBooks.aspx"); }
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); }
public static Book ConvertToBook(this BookViewModel book) { var converted = new Book() { Id = book.Id, Description = book.Description, Author = book.Author, ISBN = book.ISBN, Title = book.Title, Website = book.Website }; return converted; }
private static Category CreateOrGetCategory(BookViewModel book, LibraryDbContext db, Book newBook, Category category) { if (category == null) { category = db.Categories.Add(new Category() { Name = book.CategoryName }); category.Books = new HashSet<Book>(); category.Books.Add(newBook); } else { newBook.Category = category; } return category; }
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(""); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, BooksViewModel book) { if (book != null && ModelState.IsValid) { var category = db.Categories.FirstOrDefault(c => c.Id == book.Category.Id); var bookToCreate = new Book(); bookToCreate.Id = book.Id; bookToCreate.Author = book.Author; bookToCreate.Category = category; bookToCreate.Description = book.Description; bookToCreate.ISBN = book.ISBN; bookToCreate.Title = book.Title; bookToCreate.WebSite = book.WebSite; db.Books.Add(bookToCreate); db.SaveChanges(); } return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()); }
public ActionResult Create([DataSourceRequest]DataSourceRequest request, BookVM model) { if (ModelState.IsValid) { Book book = new Book(); book = FromBook(model); try { db.Books.Add(book); db.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("DatabaseException", "Database error while updating the entries. Try updating again." + ex.Message); } } return Json((new[] { model }.ToDataSourceResult(request, ModelState)), JsonRequestBehavior.AllowGet); }
protected void btnSave_Click(object sender, EventArgs e) { if (btnSave.CommandName != "Delete") { if (string.IsNullOrEmpty(txtTitle.Text)) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("Title is required!"); } if (txtTitle.Text.Length > 256) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("Title field cannot be more than 256 length"); } if (string.IsNullOrEmpty(txtAuthor.Text)) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("Author is required!"); } if (txtAuthor.Text.Length > 256) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("Author field cannot be more than 256 length"); } if (txtIsbn.Text.Length > 256) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("ISBN field cannot be more than 256 length"); } if (txtWebSite.Text.Length > 256) { isValidationError = true; ErrorSuccessNotifier.AddErrorMessage("Website field cannot be more than 256 length"); } } if (!isValidationError) { var categoryStatus = ActionType.Modified; var db = new ApplicationDbContext(); var book = db.Books.Find(btnSave.CommandArgument.ToInt()); if (book == null && btnSave.CommandName != "Delete") { book = new Book(); db.Books.Add(book); categoryStatus = ActionType.Created; } if (btnSave.CommandName == "Delete") { db.Books.Remove(book); categoryStatus = ActionType.Deleted; } else { var website = txtWebSite.Text; if (!website.StartsWith("http://") && !website.StartsWith("https://")) { website = "http://" + website; } book.Title = txtTitle.Text; book.Author = txtAuthor.Text; book.Isbn = txtIsbn.Text; book.WebSite = website; book.Description = txtDescription.Text; book.CategoryId = ddlCategory.SelectedValue.ToInt(); } db.SaveChanges(); grdBooks.DataBind(); ErrorSuccessNotifier.AddSuccessMessage("Book " + categoryStatus); CreatePanelVisibility(false); } }
public ActionResult Edit(Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", book.CategoryId); return View(book); }
protected void LinkButtonCreate_Click(object sender, EventArgs e) { if (!IsPostBack) { return; } string bookTitle = this.TextBoxNewBook.Text; if (string.IsNullOrWhiteSpace(bookTitle)) { this.LabelErrorMessage.Text = "Book title is required."; this.PanelNewBook.Visible = true; return; } var book = new Book { Title = bookTitle }; var context = new ApplicationDbContext(); context.Books.Add(book); context.SaveChanges(); this.PanelNewBook.Visible = false; this.TextBoxNewBook.Text = string.Empty; this.LinkButtonNewBook.Visible = true; Response.Redirect(Request.RawUrl); }
protected void LinkButtonCreateConfirm_Click(object sender, EventArgs e) { var context = new LibrarySystemEntities(); var newBook = new Book(); newBook = this.LoadBookData(newBook, context); if (newBook == null) { return; } try { context.Books.Add(newBook); context.SaveChanges(); this.GridViewBooks.DataBind(); this.TextBoxCreateEditBookAuthors.Text = string.Empty; this.TextBoxCreateEditBookDescription.Text = string.Empty; this.TextBoxCreateEditBookIsbn.Text = string.Empty; this.TextBoxCreateEditBookTitle.Text = string.Empty; this.TextBoxCreateEditBookWebSite.Text = string.Empty; ErrorSuccessNotifier.AddSuccessMessage("Book successfully created!"); } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } }
private Book LoadBookData(Book book, LibrarySystemEntities context) { var bookTitle = this.TextBoxCreateEditBookTitle.Text.Trim(); if (bookTitle.Length < 2) { ErrorSuccessNotifier.AddErrorMessage("Book title must consist of at least 2 symbols!"); return null; } var bookAuthors = this.TextBoxCreateEditBookAuthors.Text.Trim(); if (bookAuthors.Length < 2) { ErrorSuccessNotifier.AddErrorMessage("Book authors must consist of at least 2 symbols!"); return null; } var bookIsbn = this.TextBoxCreateEditBookIsbn.Text; if (!string.IsNullOrWhiteSpace(bookIsbn)) { bookIsbn = bookIsbn.Trim(); } var bookWebSite = this.TextBoxCreateEditBookWebSite.Text; if (!string.IsNullOrWhiteSpace(bookWebSite)) { bookWebSite = bookWebSite.Trim(); var match = Regex.Match( bookWebSite, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?", RegexOptions.IgnoreCase); if (!match.Success) { ErrorSuccessNotifier.AddErrorMessage("Invalid web site!"); return null; } if (!bookWebSite.StartsWith("http://") && !bookWebSite.StartsWith("https://")) { bookWebSite = string.Format("http://{0}", bookWebSite); } } var bookDescription = this.TextBoxCreateEditBookDescription.Text; if (!string.IsNullOrWhiteSpace(bookDescription)) { bookDescription = bookDescription.Trim(); } var bookCategory = context.Categories.FirstOrDefault( c => c.Name == this.DropDownListBookCategories.SelectedValue); if (bookCategory == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occurred! The category you chose for the book was not found..."); return null; } book.Title = bookTitle; book.Authors = bookAuthors; book.WebSite = bookWebSite; book.ISBN = bookIsbn; book.Description = bookDescription; book.Category = bookCategory; return book; }
private void InitializeEditPanel(int id, LibrarySystemEntities context, Book book) { this.PanelCreateBook.Visible = false; this.PanelDeleteBook.Visible = false; this.LinkButtonCreateNewBook.Visible = false; this.HiddenFieldEditBookId.Value = id.ToString(); this.TextBoxEditBookTitle.Text = book.Title; this.TextBoxEditBookAuthor.Text = book.Author; this.TextBoxEditBookISBN.Text = book.ISBN; this.TextBoxEditBookWebSite.Text = book.WebSite; this.TextBoxEditBookDescription.Text = book.Description; this.DropDownListEditBookCategory.DataSource = context.Categories.ToList(); this.DropDownListCreateBookCategory.SelectedValue = book.CategoryId.ToString(); this.DropDownListEditBookCategory.DataBind(); var categoryItem = this.DropDownListEditBookCategory.Items.FindByText(book.Category.Title); categoryItem.Selected = true; }
protected void LinkButtonSaveBook_Click(object sender, EventArgs e) { try { LibrarySystemEntities context = new LibrarySystemEntities(); Book book = new Book(); book.Title = this.TextBoxCreateBookTitle.Text; book.Author = this.TextBoxCreateBookAuthor.Text; book.ISBN = this.TextBoxCreateBookISBN.Text; book.WebSite = this.TextBoxCreateBookWebSite.Text; book.Description = this.TextBoxCreateBookDescription.Text; book.CategoryId = Convert.ToInt32(this.DropDownListCreateBookCategory.SelectedValue); context.Books.Add(book); context.SaveChanges(); HideAllPanels(); this.GridViewBooks.DataBind(); HideAllPanels(); ErrorSuccessNotifier.AddSuccessMessage("Book created."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
private static void UpdateBookFields(BookViewModel book, Book oldBook) { oldBook.Author = book.Author; oldBook.Description = book.Description; oldBook.ISBN = book.ISBN; oldBook.Title = book.Title; oldBook.Website = book.Website; }
protected void CreateBtn_Click(object sender, EventArgs e) { if (IsValid) { var context = new LibrarySystemEntities(); var title = this.BookTitleTb.Text; var author = this.BookAuthorTb.Text; var isbn = this.BookISBNTb.Text; var webSite = this.BookWebSite.Text; var description = this.BookDescriptionTa.Value; int categoryId = int.Parse(this.AllCategoriesList.SelectedValue); Book book = new Book() { Title = title, Author = author, ISBN = isbn, WebSite = webSite, Description = description, CategoryId = categoryId }; try { context.Books.Add(book); context.SaveChanges(); this.BooksListView.DataBind(); this.InsertForm.Visible = false; this.BookTitleTb.Text = ""; this.BookAuthorTb.Text = ""; this.BookISBNTb.Text = ""; this.BookWebSite.Text = ""; this.BookDescriptionTa.Value = ""; ErrorSuccessNotifier.AddSuccessMessage("Book Added"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("../EditCategories.aspx"); } } }
public ActionResult BooksDelete([DataSourceRequest]DataSourceRequest request, Book book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Books.Attach(book); db.Books.Remove(book); db.SaveChanges(); } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }