ASP. net mvc + LINQ develop a book sales site (7): Book Category Management

Source: Internet
Author: User

1. Browsing Classification

A. Modify contoller as follows:

 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web; Using System. Web. MVC; Using Bookshop. models; // Import Model  Namespace Bookshop. controllers { Public   Class Categorycontroller: controller {bookshopdbdatacontext DB = New Bookshopdbdatacontext (); // Category/List Public   Void List () {list <Category> categories = dB. getallcategory (); renderview ( "Categorylist" , Categories );} // Category/edit/ID  Public   Void Edit ( Int ID ){} // Category/delete/ID  Public   Void Delete ( Int ID ){} // Category/Add  Public   Void Add (){}}}
B. Create a corresponding category folder under the View File and create a (MVC View content page) categorylist. aspx
 
 
 
C. Modify categorylist. aspx. CS as follows:Code:
 
 
 
D. Modify the category. aspx view.
 
 
 
E. Browse (because there is no data in the database, as shown in)
 
 
 
2. Add a directory
 
A. Now we can implement the new function, modify the Add action of categorycontroller, create an addsaved action, save the newly created directory, and navigate to the List View.

// Category/Add
Public   Void Add ()
{
Renderview (
" Addcategory " );
}

Public   Void Addsaved ()
{
CATEGORY newcategory
=   New CATEGORY {categoryname = Request. Form [ " Categoryname " ]};
DB. addcategory (newcategory );
Redirecttoaction (
New Routevaluedictionary ( New {Controller =   " Category " , Action =   " List " }));
}

B. Create an addcategory. aspx (MVC View content page) under view \ category \ to create a new view.

C. Final Results

3. Modify the directory:

A. Add the following two methods to the bookshopdbdatacontext partial class:

// Edit category
Public   Void Editcategory (category C)
{
This . Updatecategory (C );
This . Submitchanges ();
}

PublicCATEGORY getcategory (IntID)
{
ReturnCategories. Single (C=>C. categoryid=ID );
}

B. Add the following method to categorycontroller.

// Category/edit/ID
Public   Void Edit ( Int ID)
{

Renderview ( " Editcategory " , DB. getcategory (ID ));
}
Public   Void Editsaved ( Int ID)
{
Category C
= DB. getcategory (ID );
C. categoryname
= Request. Form [ " Categoryname " ];
// Bindinghelperextensions. updatefrom (C, request. form );
DB. editcategory (C );

List < Category > Categories = DB. getallcategory ();
Redirecttoaction (
New Routevaluedictionary ( New {Controller =   " Category " , Action =   " List " }));
}

C. Create an editcategory. aspx (MVC View content page) under view \ category \ to create a new view.

Modify categorylist. aspx

Modify editcategory. aspx. CS as follows:

Modify editcategory. aspx as follows:

D. effect:

 
 
 
4. delete a directory
 
A. Modify categorylist. aspx
 
 
 
B. Modify categorycontroller and add
 
 
 
C. Effect
 
 
 
Not complete...
 
 
 
 
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.