Library Management using ASP.Net MVC (Part 1) Books CRUD Function

This tutorial is about creating a library management system using asp.net mvc and in this part you will learn how to create a Model for your data, add the needed data annotation and scaffold a basic crud application for your model.

Before we start coding we should identify first the user’s problem that we are going to solve.

As a librarian

  • As a librarian I should be able to add new books.
  • As a librarian I should be able to update book details.
  • As a librarian I should be able to remove a book.
  • As a librarian I should be able to see the list of books and borrowed books.
  • As a librarian I should be able to add a new customer.
  • As a librarian I should be able to assign a borrowed book to a customer.
  • As a librarian I should be able to view customer’s borrowing history.

The list above are some of the problems that we must solve for a librarian. There are still some problems that can be added in the list, but I want to keep to minimum.

Create a new MVC project using Visual Studio, I called my project as LibraryManagement. I’m using a visual studio 2017, but things that are written here should also work in earlier version of visual studio.

create a new project

Now choose MVC as a template and make sure that you change the authentication to Individual user accounts.

Mvc project

After creating a new project, try to run it and you should have this output in your browser.

new project first run

If you got that page, then everything is working for you. Now it’s time for us to go in to coding. Stop your application and open the Models folder in the solution explorer.

If you are not familiar with MVC architecture. For now you can think of a Model as a representation of your table in a database, actually it’s more than that, but for now just think of it that way.

In our library management our book table can be represented in our code as the Book class and the columns in our table are the read/write properties in our class.

Let’s start coding, so you will understand what I mean. In Models folder add a new class called Book. You can do that by right clicking in the Models folder then select add then click class.

Then update the code in your Book class to look like this.

using System.ComponentModel.DataAnnotations;

namespace LibraryManagement.Models
{
    public class Book
    {
        public int BookId { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string SerialNumber { get; set; }

        public string Author { get; set; }

        public string Publisher { get; set; }
    }
}

You might notice the [Required] in the top of the properties Title and SerialNumber. That’s called a Data Annotation. We want the Title and SerialNumber to be required field and to do that we put the Required data annotation on these properties.

I said earlier that this class is a representation of our table in the database, so you might think that the Author and Publisher should be a foreign key for Author and Publisher table. You are correct! but I want to make things simple and just focus on the basic functionality of a library management system.

Now open your IdentityModels.cs inside the Models folder.  Scroll down to  ApplicationDbContext class. As I said earlier that our models are the representation of our database tables, then you can consider the ApplicationDbContext as the representation of our database. We should add our Book model in the ApplicationDbContext to map it to our database.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public DbSet<Book> Books { get; set; }
    }

Press the Ctrl + Shift + B to build our project. The right click in our Controllers folder. Then click add then select controller. Then select the MVC 5 Controller with views, suing Entity Framework then click Add button. Then for Models select the Book and ApplicationDbContext for the data context class then click the Add button.

books scaffolding

Adding controller

What we did is we scaffold a basic Create, Read, Update and Delete (CRUD) function for books. Let’s add the link for our book in the navigation bar. Open the Views folder then Shared. You should see the _Layout.cshtml. Open it and scroll down until you find this part.

<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
	<li>@Html.ActionLink("Home", "Index", "Home")</li>
	<li>@Html.ActionLink("About", "About", "Home")</li>
	<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")</div>

We are going to add the link for books in our navigation bar. To do that we use the Html.ActionLink that accepts three parameters. The first one is the name that will be displayed in the link, then second one is the Action in our controller and the last parameter is the name of the controller. I know you might be confused about this action and controller thing, but I will explain it later, for now just add the code below inside the ul tag, below the Contact.

	<li>@Html.ActionLink("Books", "Index", "Books")</li>

After adding the link for our book, run the application.

Now you should see the link for Books in the navigation bar, click it and you will be directed to this page. This is the index page of the book controller.

books index page

Now go and play around with it, add some data. You will also noticed that our data annotation earlier really works.

insert book

Did you noticed the SerialNumber? It displayed as SerialNumber, and we don’t want that. It should be displayed as Serial Number. Let’s go back to our Book class and fixed it.

We will add another data annotation in SerialNumber property called Display. Using the Name parameter we can assign the display name for that property. Now your Book class should look like this.

public class Book
    {
        public int BookId { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        [Display(Name="Serial Number")]
        public string SerialNumber { get; set; }

        public string Author { get; set; }

        public string Publisher { get; set; }
    }

Run your application again and check if it fixed the display name problem for Serial Number.

Serial number

And now it’s fixed.

Now we already cover the basic CRUD function for books, let’s continue adding the other functionality of our library management in the next part.

Related Articles

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s