ASP.NET MVC+LINQ開發一個圖書銷售網站(7):圖書分類管理

來源:互聯網
上載者:User

1、瀏覽分類

a. 修改Contoller的為如下

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/Listpublic void List(){List<Category> categories = db.GetAllCategory();RenderView("CategoryList", categories);}// Category/Edit/idpublic void Edit(int id){}//Category/Delete/idpublic void Delete(int id){}//Category/Addpublic void Add(){}}}
b.在view檔案下建立一個對應的Category的檔案夾,在其下建立一個(MVC view content page) CategoryList.aspx
  
c. 修改CategoryList.aspx.cs為如下代碼:
 
d. 修改Category.aspx的視圖
 
e. 瀏覽(因為資料庫裡沒有資料,所以看到如)
 
2、添加目錄
a. 現在我們來實現建立的功能,修改CategoryController的Add的行為,建立一個AddSaved的行為儲存建立的目錄,並導航到List視圖

//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. 我們需要在view\category\下建一個AddCategory.aspx(MVC view content page)來建立一個視圖

c. 最終效果

3. 修改目錄:

a. 添加下面兩個方法到BookShopDBDataContext部分類別

//Edit Category
     public void EditCategory(Category c)
     {
         this.UpdateCategory(c);
         this.SubmitChanges();
     } 

     public Category GetCategory(int id)
     {
         return Categories.Single(c => c.CategoryId == id);
     } 

b. 添加下面的方法到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. 我們需要在view\category\下建一個EditCategory.aspx(MVC view content page)來建立一個視圖

修改CategoryList.aspx

 

修改EditCategory.aspx.cs如下

修改EditCategory.aspx如下

d.效果:

   
4. 刪除目錄
a. 修改CategoryList.aspx
 
b. 修改CategoryController,添加
 
c.效果
 
未完待續。。。
 
 
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.