@model OnlineLibraryMvcApp.BookViewModel
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Online Library</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div class="col-md-6 col-md-offset-3">
@using (Html.BeginForm("AddBook", "Library", FormMethod.Post, new { id = "bookForm" }))
@Html.LabelFor(m => m.Title)
@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Title)
@Html.LabelFor(m => m.Author)
@Html.TextBoxFor(m => m.Author, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Author)
@Html.LabelFor(m => m.Genre)
@Html.TextBoxFor(m => m.Genre, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Genre)
<button type="button" class="btn btn-success submit">Add Book</button>
<div class="alert alert-warning fade">
<strong><span class="alert-content"></span></strong>
<h2>Library Inventory</h2>
<ul id="bookList" class="list-group">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
function openAlert(txt) {
$('.alert-content').text(txt);
$('.alert').addClass('in');
$('.alert').removeClass('in');
$.getJSON('@Url.Action("GetBookList", "Library")', function (books) {
var bookList = $('#bookList');
$.each(books, function (i, book) {
bookList.append('<li class="list-group-item">' + book.Title + ' - ' + book.Author + ' (' + book.Genre + ')' +
'<button class="btn btn-danger btn-sm pull-right delete-book" data-id="' + book.BookId + '">Delete</button>' +
$('.submit').click(function () {
if ($('#bookForm').valid()) {
url: '@Url.Action("AddBook", "Library")',
data: $('#bookForm').serialize(),
success: function (resp) {
openAlert("An error occurred while processing your request.");
$(document).on('click', '.delete-book', function () {
var bookId = $(this).data('id');
url: '@Url.Action("DeleteBook", "Library")',
data: { bookId: bookId },
success: function (resp) {