I've added two other projects to the solution.
A class library named "BookShared".
An MVC project named "BookWebApp".
The class library contains a data folder and the models folder.
They are shared (and referenced) by the console application and the webApp.
Entity Framework was downloaded for all of the projects.
The database is now being initialised in the web.config file.
The web.config file also contains the database connection string.
The default constructor in the controller class instantiates a context instance when the controller is instantiated.
The index method contains LINQ statements which get a list of the books from the database.
The information is passed to the view.
Once the instance of the database context class is not needed anymore the dispose method is called.
In this configuration, the lifetime of the instance of the context is similar to the instance of the controller.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
Each record displayed in the “Index” view contains its own “Detail” button.
When the detail button is clicked by the USER the id associated with the record is passed to the detail action method.
If the id sent to the server is NULL an error message is returned to the client (HttpStausCode.BadRequest).
If the id is not NULL the variable “book” get loaded with the book data for the record that matches the Id in the database.
After this if “book” is NULL (possibly caused by the Id not matching the Id of any of the records in the database) another error message is rendered (HttpNotFound).
If Id is not NULL and matches a record in the database then the data is passed to the view.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
Clicking on the "Add Book" button triggers the action method “Add()”.
An instance of the view model for “adding books” is instantiated.
The context object gets passed to the initialise method in the “add book” view model.
The context object gets passed to the initialise method in the base “add book” view model.
In the base “add book” view model, the select list for the “Authors” are retrieved from the database.
In the “add book” view model, the two select lists for “Genres” and “Fiction/Non-Fiction” are retrieved from the database.
Finally the “add book” view model is sent to the view and the HTML is rendered.
The following 8 screenshots show this process in more detail.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
The user enters some data and clicked on the “Save” button.
the “Book Add” view model data is posted to the “add post” method.
This data is loaded to a variable named “book”.
The “AddGenre” method is called passing in the required parameters.
The book is added to the context object.
The changes are saved.
Thee “detail” view for the new record is rendered.
The following 6 screenshots show this process in more detail.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
When the data for one of the records (books) is displayed (books/detail/Id) and the USER clicks on the “edit” button.
The Id of the records gets passed to the edit action method.
If the server doesn’t receive the Id an error message is returned.
If the Id is not NULL it is used to query the database.
If a record exists in the database with a matching Id, the record gets loaded to the variable “book”.
The book constructor gets called.
If a record with a matching Id doesn’t exist in the database, HttpNotFound().
If it does a “Books Edit” view model instance is created, the book constructor is called and the book variable which is holding the data for the record with the matching Id is loaded to the view model’s book parameter.
The view model is passed to the view (Books/Edit/Id).
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
When the USER clicks tries to edit a record (book) they are directed to the view “…Books/Edit/Id”
Once the USER changes the data and clicks on the “save” button, the “Books Edit” view model data is passed to the edit POST action method.
The “edited record data” is loaded to a variable.
The variable is used to update the record inside the database.
The changes to the database are saved.
The USER is then redirected to the “detail” view of the new record, displaying the updated data.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
When the USER is viewing the data for one of the records (book) and they click on the “delete” button, the Id of the record is sent to the “delete” action method.
If the id sent to the server is NULL an error message is returned to the client (HttpStausCode.BadRequest).
If the id is NOT NULL then the id is used to retrieve the record from the database.
The book constructor is called.
The data is loaded to “var book”.
If a record matching that id, is not found, HttpNotFound() is returned.
The data for the record is passed to the view.
1: Index() – Displays all books in database.
2: Detail() – Displays all info for a selected book.
3: Add() – Displays view for USER to enter data.
4: Add() POST – Saves data to database.
5: Edit() – Displays view for USER to edit record.
6: Edit() POST – Saves edited data to database.
7: Delete() – Displays view for a selected book.
8: Delete() POST – Deletes record from database.
The USER is prompted to check if they are sure that they want to delete the record (book).
If the USER clicks on “delete” button the id for the record gets passed to the “delete” POST action method.
An instance of a new book object is created with the same Id that was posted.
The record is deleted from the database.
The changes to the database are saved.
The USER is redirected to a list of all the books (…/Books/Index).