zoukankan      html  css  js  c++  java
  • ASP.NET MVC+LINQ开发一个图书销售站点(7):图书分类管理

    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/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.在view文件下建立一个对应的Category的文件夹,在其下建立一个(MVC view content page) CategoryList.aspx
    image image 
    c. 修改CategoryList.aspx.cs为如下代码:
    image 
    d. 修改Category.aspx的视图
    image 
    e. 浏览(因为数据库里没有数据,所以看到如下图)
    image 
    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)来新建一个视图

    image

    c. 最终效果

    image

    image

    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

    image image

    修改EditCategory.aspx.cs如下

    image

    修改EditCategory.aspx如下

    image

    d.效果:

      image 
    4. 删除目录
    a. 修改CategoryList.aspx
    image 
    b. 修改CategoryController,添加
    image 
    c.效果
    image 
    未完待续。。。
     
     
     

     

    扫码关注公众号,了解更多管理,见识,育儿等内容

    作者: 王德水
    出处:http://www.cnblogs.com/cnblogsfans
    版权:本文版权归作者所有,转载需经作者同意。

  • 相关阅读:
    读书笔记:A Philosophy of Software Design
    面向对象编程—价值万亿美元的灾难
    刚哥谈架构 (二) 我眼中的架构师
    软件质量成本神话
    API 如何选择 REST,GraphQL还是gRPC
    影响您的代码库的10个编程代码味道
    为什么要不断重构
    php导出excel表格的使用
    浅谈HTTP中Get与Post的区别
    C# 程序配置文件的操作(ConfigurationManager的使用)
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1123037.html
Copyright © 2011-2022 走看看