目录
效果图
实现过程
1创建数据库
2创建项目文件
3创建控制器,右键添加,控制器
编辑 注意这里要写Home编辑
创建成功
数据模型创建过程之前作品有具体过程编辑
4创建DAL
5创建BLL
6创建视图,右键添加视图
编辑 7HomeController.cs代码
8Index.cshtml代码
效果图


实现过程
1创建数据库

create database GoodsDB
CREATE TABLE Goods(
	[GoodsID] [int] IDENTITY(1,1) NOT NULL,
	[GoodsName] [nvarchar](50) NOT NULL,
	[GoodsPrice] [decimal](10, 2) NOT NULL,
	[GoodsStock] [int] NOT NULL)
	INSERT  Goods( [GoodsName], [GoodsPrice], [GoodsStock]) VALUES ( N'魔幻陀螺', CAST(19.00 AS Decimal(10, 2)), 20)
INSERT Goods( [GoodsName], [GoodsPrice], [GoodsStock]) VALUES ( N'开花精灵惊喜盲盒', CAST(89.00 AS Decimal(10, 2)), 10)
INSERT Goods ( [GoodsName], [GoodsPrice], [GoodsStock]) VALUES ( N'跳跃战士玩具2变形汽车', CAST(18.00 AS Decimal(10, 2)), 50)
INSERT Goods( [GoodsName], [GoodsPrice], [GoodsStock]) VALUES ( N'正版灵动奥特曼软胶囊玩具', CAST(25.00 AS Decimal(10, 2)), 20)
INSERT Goods ( [GoodsName], [GoodsPrice], [GoodsStock]) VALUES ( N'帮帮龙变形车套装', CAST(109.00 AS Decimal(10, 2)), 5)
2创建项目文件
 
 
 
3创建控制器,右键添加,控制器

 注意这里要写Home
 注意这里要写Home
 
创建成功
数据模型创建过程之前作品有具体过程
 
4创建DAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;
namespace MvcApplication1.DAL
{
    public class GoodsService
    {   
        //展示
        public static List<Goods> Show() {
            GoodsDBEntities db = new GoodsDBEntities();
            return db.Goods.ToList();
        }
        //计算小计
        public static string Sun() {
            GoodsDBEntities db = new GoodsDBEntities();
            return db.Goods.Sum(m => m.GoodsPrice * m.GoodsStock).ToString();
        }
        //找到需要删除的一条数据,后面被调用
        public static Goods GetID(string goodid) {
            GoodsDBEntities db = new GoodsDBEntities();
            int temp = Convert.ToInt32(goodid);
            return db.Goods.SingleOrDefault(m => m.GoodsID == temp);
        }
        //调用查找数据的方法进行删除
        public static bool Delect(string goodid) {
         Goods good=   GetID(goodid);
         if (good==null)
         {
             return false;
         }
         else
         {
             GoodsDBEntities db = new GoodsDBEntities();
             db.Entry(good).State = System.Data.EntityState.Deleted;
             db.Goods.Remove(good);
             return db.SaveChanges() > 0;
         }
        }
        //排序方法
        public static List<Goods> getGoodlist(bool priceOrder) {
            GoodsDBEntities db = new GoodsDBEntities();
            if (priceOrder==true)
            {
                return db.Goods.OrderByDescending(m => m.GoodsPrice).ToList();
            }
            else
            {
                return db.Goods.OrderBy(m => m.GoodsPrice).ToList();
            }
        
        
        }
    }
}5创建BLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;
namespace MvcApplication1.BLL
{
    public class GoodsManger
    {
        public static List<Goods> Show()
        {
            GoodsDBEntities db = new GoodsDBEntities();
            return db.Goods.ToList();
        }
        public static string Sun()
        {
            return DAL.GoodsService.Sun();
        }
        public static bool Delect(string goodid) {
            return DAL.GoodsService.Delect(goodid);
        
        }
        public static List<Goods> getGoodlist(bool priceOrder) {
            return DAL.GoodsService.getGoodlist(priceOrder);
        }
    }
}6创建视图,右键添加视图
 7HomeController.cs代码
 7HomeController.cs代码
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index(bool PriceOrderDese=false)
        {
        ViewData["goods"] = BLL.GoodsManger.Show();
       ViewData["sum"] = BLL.GoodsManger.Sun();
       ViewData["goods"] = BLL.GoodsManger.getGoodlist(PriceOrderDese);
       ViewBag.PriceOrderDese = !PriceOrderDese;
       return View();
        }
        public ActionResult DeleteGood(string goodId) {
            BLL.GoodsManger.Delect(goodId);
            return RedirectToAction("Index");
        }
    }
}
8Index.cshtml代码

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table border="1">
            <tr>
                <th>ID</th>
                 <th>商品名</th>
                    <th>@Html.ActionLink("价格排序", "Index", new {PriceOrderDese=@ViewBag.PriceOrderDese})</th>
                 <th>库存</th>
                 <th>小计</th>
                 <th>操作</th>
            </tr>
            @{
                foreach (var good in ViewData["goods"] as List<MvcApplication1.Models.Goods>)
                {
                     <tr>
                <td>@good.GoodsID</td>
                 <td>@good.GoodsName</td>
                 <td>@good.GoodsPrice</td>
                 <td>@good.GoodsStock</td>
                 <td>@(@good.GoodsPrice*@good.GoodsStock)</td>
                 <td>@Html.ActionLink("删除","DeleteGood", new{goodId=good.GoodsID})</td>
            </tr>
                }
                
                
                
                }
            <tr>
                <td  colspan="6">总计:@ViewData["sum"]</td>
                
            </tr>
        </table>
    </div>
</body>
</html>



















