[. NET] building a simple MVC e-commerce website step by step, mvc e-commerce
Build a simple MVC e-commerce website step by step-BooksStore (4)
GitHub address: https://github.com/liqingwen2015/Wen.BooksStore
Building a simple MVC e-commerce website step by step-BooksStore (1) (released on)
Building a simple MVC e-commerce website step by step-BooksStore (II) (released on)
Building a simple MVC e-commerce website step by step-BooksStore (3) (released on)
Basic addition, deletion, modification, and query CRUD
We create a new controller to add, delete, modify, and query functions, AdminController, and add a method to display all data:
/// <Summary> /// background management Controller /// </summary> public class AdminController: Controller {private readonly IBookRepository _ bookRepository; public AdminController (IBookRepository bookRepository) {_ bookRepository = bookRepository;} // <summary> // home /// </summary> /// <returns> </returns> public ActionResult Index () {return View (_ bookRepository. books );}}
If the previous layout page is not followed, create a new layout page _ AdmindLayout. cshtml:
<! DOCTYPE html>
Corresponding Index. cshtml:
@ Model IEnumerable <Wen. BooksStore. Domain. Entities. Book> @ {Layout = "~ /Views/Shared/_ AdminLayout. cshtml ";}< p> @ Html. actionLink ("add", "Edit ") </p> <table class = "table"> <tr> <th> name </th> <th> description </th> <th> price </th> <th> Category </th> </tr> @ foreach (var item in Model) {<tr> <td> @ Html. displayFor (modelItem => item. name) </td> <td> @ Html. displayFor (modelItem => item. description) </td> <td> @ Html. displayFor (modelItem => item. price) </td> <td> @ Html. displayFor (modelItem => item. category) </td> <td> @ Html. actionLink ("Edit", "Edit", new {id = item. id}) @ using (Html. beginForm ("Delete", "Admin", FormMethod. post, new {style = "display: inline;"}) {@ Html. hidden ("id", item. id) <input type = "submit" value = "delete"/>}</td> </tr >}</table>
/// <Summary> /// edit /// </summary> /// <param name = "id"> </param> /// <returns> </ returns> public ActionResult Edit (int id = 0) {if (id = 0) {return View (new Book ();} var model = _ bookRepository. books. firstOrDefault (x => x. id = id); return View (model );} /// <summary> /// edit /// </summary> /// <param name = "book"> </param> /// <returns> </ returns> [HttpPost] public ActionResult Edit (Book book) { If (! ModelState. IsValid) {return View (book);} _ bookRepository. SaveBook (book); return RedirectToAction ("Index ");}
How to update a repository:
/// <Summary> /// book repository interface /// </summary> public interface IBookRepository {/// <summary> // book model set /// </summary> IQueryable <Book> Books {get ;} /// <summary> /// Save the book /// </summary> /// <param name = "book"> </param> /// <returns> </returns> int SaveBook (Book book ); /// <summary> /// Delete the book /// </summary> /// <param name = "id"> </param> /// <returns> </returns> Book DeleteBook (int id );}
EfBookRepository. cs
/// <Summary> /// book repository /// </summary> public class EfBookRepository: IBookRepository {private readonly EfDbContext _ context = new EfDbContext (); /// <summary> /// Book model set /// </summary> public IQueryable <Book> Books => _ context. books; /// <summary> /// Save the book /// </summary> /// <param name = "book"> </param> /// <returns> </returns> public int SaveBook (Book book) {if (book. id = 0) {_ context. books. add (book);} else {var model = _ context. books. find (book. id); if (model = null) {return 0;} model. category = book. category; model. description = book. description; model. name = book. name; model. price = book. price;} return _ context. saveChanges ();} /// <summary> /// Delete the book /// </summary> /// <param name = "id"> </param> /// <returns> </returns> public Book DeleteBook (int id) {var model = _ context. books. find (id); if (model = null) {return null;} _ context. books. remove (model); _ context. saveChanges (); return model ;}}
The following features must be added to the Book Model:
[Table ("Book")] public class Book {// <summary> // Id /// </summary> public int Id {get; set ;} /// <summary> /// Name /// </summary> [Required (ErrorMessage = "Name cannot be blank")] public string Name {get; set ;} /// <summary> /// Description /// </summary> [Required (ErrorMessage = "Description cannot be blank")] public string Description {get; set ;} /// <summary> /// price /// </summary> [Required (ErrorMessage = "price cannot be blank")] [Range (0.01, double. maxValue, ErrorMessage = "please fill in the appropriate Price")] public decimal Price {get; set ;} /// <summary> /// Category /// </summary> [Required (ErrorMessage = "Category cannot be blank")] public string Category {get; set ;}}
_ AdminLayout. cshtml introduce the JavaScript code for verification (client verification ):
<script src="~/Scripts/jquery-1.10.2.js"></script> <script src="~/Scripts/jquery.validate.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
Edit. cshtml
@ Model Wen. BooksStore. Domain. Entities. Book @ {Layout = "~ /Views/Shared/_ AdminLayout. cshtml ";}< h2> edit
Figure: error message
/// <Summary> /// Delete /// </summary> /// <param name = "id"> </param> /// <returns> </ returns> [HttpPost] public ActionResult Delete (int id) {_ bookRepository. deleteBook (id); return RedirectToAction ("Index ");}
Add prompt. When adding, editing, and deleting, we should add the necessary prompt information and use TempData.
Add the following items in/Admin/Index. cshtml:
Execution result:
[Note] temporary TempData data stores a piece of information, which is a "key/value" dictionary, similar to Session and ViewBag. The difference between it and Session is, the HTTP request will be deleted after it is completed. Because RedirectToAction is used here, A redirection command will tell the browser to redirect requests to a new address, so ViewBag cannot be used, and ViewBag is used to transmit data between the Controller and the view, however, it cannot keep the data longer than the current HTTP request. Redirection means that the user is cross-request and ViewBag cannot be used to transmit data across requests.