# MVC Architecture
This material is part of Learning Objective 5 (minimum and desired).
### Exercise preparation: JavaScript
**SQL Server Setup**
Do the following in SQL Server Management Studio:
- use the script to create database, its structure and some test data: https://pastebin.com/jtJfak9E
- in the script, **change the database name to Exercise14 and use it**
- execute it to create database, its structure and some test data
- execute additional script 1: https://pastebin.com/SeHBs1BA
- execute additional script 2: https://pastebin.com/7qHM2h2d
**Project starter**
Unpack the project starter archive and open the solution in the Visual Studio.
Set up the connection string and run the application.
Check if the application is working (e.g. navigation, list of songs, adding a new song).
> In case it's not working, check if you completed the instructions correctly.
**Application users**
Admin: username is admin, password is 12345678
User: username is user1, password is 12345678
### 1. JavaScript Folders
By default, `wwwroot` folder contains the following JS folders:
- `lib`
- `js`
**lib folder**
The 'lib' folder is one of common folders (also in ASP.NET Core MVC template) for 3rd party JS libraries. By default, it contains the following libraries:
- Bootstrap
- jQuery
- jQuery validation
- jQuery unobtrusive validation
ASP.Net Core MVC template uses Bootstrap and jQuery in layout view `_Layout.cshtml`, and so in every page.
**js folder**
The 'js' folder is a folder for your own scripts. There is `site.js` script file that is already added to the project with the default template. It is also included in `_Layout.cshtml` so that it's available in every page. For example, you can write utility functions in the `site.js` file, or write script that dynamically extends functionality of navigation bar.
> For page-specific logic, it's a common practice to use per-page JS files. For example, for `Product/Add` route use `js/product/add.js` JS file.
### 2. JavaScript example: dynamically creating toasts
Let's use TempData and Bootstrap to signal the user that he just created a new genre. It can be done using "toast" component. Bootstrap library implements such component.
```JavaScript
// In site.js
// Template HTML generator function for toast message
const createToastTemplate = (title, info, body) =>
`
${title}
${info}
${body}
`
// Function that creates toast DOM from HTML and instances a new Bootstrap toast
function createToast(title, info, body, delay = null) {
const toastTemplate = createToastTemplate(title, info, body);
const placeholder = document.createElement("div");
placeholder.innerHTML = toastTemplate;
const toastEl = placeholder.firstElementChild;
var toastPh = document.getElementById('toast-placeholder');
toastPh.append(toastEl);
var visibleToast = new bootstrap.Toast(toastEl, { 'autohide': true, 'delay': delay });
visibleToast.show();
}
```
```HTML
```
Mark "alert" Bootstrap element for the new genre with classes `new-genre` and `d-none`. Class `d-none` will hide the "alert" but also render it in the DOM. When page is loaded, JavaScript can target the hidden element using `new-genre` class, read its text and show it as "toast".
```JavaScript
@*In Genre/Index.cshtml, add Scripts section*@
@section Scripts {
}
```
### 3. JavaScript: LibMan
LibMan is a popular tool that is able to install and uninstall 3rd party client JS libraries. It can also remember which libraries should be install and "clean" them from solution in order to reduce the solution archive size. You can use "restore" functionality later to install back the deleted JS files.
LibMan is built into the Visual Studio and can be used from the context menu.
Let's install an example JavaScript library "Backpax" using LibMan:
**Install Backpax**
- In Solution explorer, in project context menu select "Add > Client-Side Library"
- Select "jsdelivr", search for "backpax" and install it
- Reference: https://appleple.github.io/backpax/ (find it in README.md in the backpax folder)
- Add following code to `_Layout.cshtml`
```HTML
```
- Add following code to `Home/Index.html`
```HTML
Scroll down...
```
- Before that code add few paragraphs generated on https://www.lipsum.com/ with `font-size` set to `x-large` to have enough content height to see the result
- Activate `Backpax` parallax scrolled image
```JavaScript
@section Scripts {
}
```
- Test the functionality
### 4. Transforming multiselect `