Adding an Admin View
(1) Now that set up CRUD function in WEBAPI, we will turn to client side and add pages that can consume the data.
Go to HomeController, add an [Admin] method.
Create a new View for this new Controller
(2) Update Admin.cshtml view per the instructions.
Open /Views/Shared/_Layout.cshtml, update the shared “master page” to add a link to the /Home/Admin
Clicking on [Admin] you get this:
However, Admin page is only meant for Admin, so add authorization.
To Add the Administrator (role) and Administrator (user name), open this file:
And Add these lines to check/create the roles/user. (quick and dirty to add user/role; not good practice)
In /Home/Admin (action), allow only Administration Role
In the /Admin (controller), allow only Administrator
Difference between WebAPI and MVC’s [Authorize]
Web API: System.Web.Http.AuthorizeAttribute
Creating a Dynamic UI with Knockout.js
KO makes it easy to bind HTML to data.
First we define [ViewModel]. Add this to Admin.cshtml to include the KO library.
Inside the empty <script> tag, we will define the ViewModel:
Outside of ProductViewModel(), do this:
So the binding can occur as soon as all HTML/script loaded.
Create a ProductDTO to hide the properties that you want to hide from the public – such as ActualPrice.
Then create a ProductController,
The MapProducts() returns a IQuerable, so we can use it in other queries.
Create another DTO: OrderDTO. (nested classes?)
Add a new OrdersController
Creating Main Page
Open /Home/index.cshtml, replace all HTML as indicated.
A typical jquery AJAX POST POST – success and error