管理控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Entities; using SportsStore.Domain.Abstract; namespace SportsStore.WebUI.Controllers
{ public class AdminController : Controller
{
private IProductRepository repository; public AdminController(IProductRepository repo)
{
repository = repo;
} public ViewResult Index()
{
return View(repository.Products);
} public ViewResult Edit(int productId)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
return View(product);
} // 专门接受post请求处理的
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid) // 检测数据是否合法
{
repository.SaveProduct(product); // 调用保存数据的方法
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index"); // 跳转到Index页面
}
else
{
return View(product);
}
} public ViewResult Create()
{
return View("Edit", new Product());
}
}
}

列表页面

@model IEnumerable<SportsStore.Domain.Entities.Product>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
} <h2>全部商品</h2> <p>
@Html.ActionLink("添加一个新的产品", "Create",null,new {@class = "btn btn-default" })
</p>
<table class="table">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Price
</th>
<th>
Action
</th>
<th></th>
</tr> @foreach (var item in Model) {
<tr>
<td>
@item.ProductID
</td>
<td>
@Html.ActionLink(item.Name, "Edit", new { item.ProductID })
@*默认就是ProductID*@
@*超链接*@
</td>
<td>
@item.Price.ToString("c")
</td>
<td>
@using (Html.BeginForm("Delete","Admin"))
{
@Html.Hidden("ProductId", item.ProductID)
//创建传参的隐藏元素
<input type="submit" class="btn btn-default btn-xs" value="Delete">
}
@*@Html.ActionLink("Delete", "Delete", new { id=item.ProductID })*@
</td>
</tr>
} </table>

修改页面

@model SportsStore.Domain.Entities.Product
@{
ViewBag.Title = "Admin: Edit " + @Model.Name;
Layout = "~/Views/Shared/_AdminLayout.cshtml";
HtmlHelper.ClientValidationEnabled = false;
HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
} <div class="panel">
<div class="panel-heading">
<h3>Edit @Model.Name</h3>
</div> @*@using (Html.BeginForm())*@
@*设置固定的跳转目录*@
@using (Html.BeginForm("Edit", "Admin"))
{
<div class="panel-body">
@Html.HiddenFor(m => m.ProductID)
@foreach (var property in ViewData.ModelMetadata.Properties)
{
if (property.PropertyName != "ProductID")
{
<div class="form-group">
<label>@(property.DisplayName ?? property.PropertyName)</label>
@if (property.PropertyName == "Description")
{
@Html.TextArea(property.PropertyName, null,
new { @class = "form-control", rows = 5 })
}
else
{
@Html.TextBox(property.PropertyName, null,
new { @class = "form-control" })
} @*提示错误的验证消息*@
@Html.ValidationMessage(property.PropertyName) </div>
}
}
</div> <div class="panel-footer">
<input type="submit" value="Save" class="btn btn-primary" />
@Html.ActionLink("Cancel and return to List", "Index", null, new
{
@class = "btn btn-default"
})
</div>
}
</div>

产品模型修饰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc; namespace SportsStore.Domain.Entities
{
public class Product
{
[HiddenInput(DisplayValue = false)]
// 隐藏起来,不显示在编辑选项中
public int ProductID { get; set; } [Required(ErrorMessage = "Please enter a product name")]
// 必填项
public string Name { get; set; } [DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Please enter a description")]
// 设置多行显示文本
public string Description { get; set; } [Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
public decimal Price { get; set; } [Required(ErrorMessage = "Please specify a category")]
public string Category { get; set; }
}
}

接口定义修改方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Entities; namespace SportsStore.Domain.Abstract
{
public interface IProductRepository
{
IEnumerable<Product> Products { get; } // 定义保存商品的方法
void SaveProduct(Product product);
}
}

实现修改方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities; namespace SportsStore.Domain.Concrete
{ public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext(); public IEnumerable<Product> Products
{
get { return context.Products; }
} // 继承接口,就要实现它的方法
public void SaveProduct(Product product)
{ if (product.ProductID == 0)
{
context.Products.Add(product);
}
else
{
Product dbEntry = context.Products.Find(product.ProductID); // 找到商品
if (dbEntry != null)
{
dbEntry.Name = product.Name;
dbEntry.Description = product.Description;
dbEntry.Price = product.Price;
dbEntry.Category = product.Category;
}
}
context.SaveChanges(); // 保存
}
}
}

方法论:理解之后,再写个博客总结一下。不然写完博客,一样不理解。注释,百度,提问,对比,总结是一个很好的学习方法和过程。

.Net商品管理(注释,百度,提问,对比,总结)的更多相关文章

  1. 「超市管理系统——商品管理」 · Java Swing + MySQL JDBC开发

    项目下载:https://download.csdn.net/download/weixin_44893902/13715024 1.9元付费赞助下载:https://download.csdn.ne ...

  2. C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试

    在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...

  3. SQL Server 【附】创建"商品管理数据库"、"学生选课数据库"的SQL语句

    附:(创建“商品管理数据库”的SQL语句) --建立"商品管理数据库"数据库-- create database 商品管理数据库 on(name='商品管理数据库_m', file ...

  4. GZFramwork快速开发框架演练之会员系统(四)添加商品管理

    1.1:创建表结构 新建三张商品关联的表,表模型如下: 创建SQL语句略 1.2:生成表Model(生成方法见上一节) 1.3:生成tb_ProductType的单结构界面然后添加到项目中 1.4:修 ...

  5. oldboy s21day12.设计商城系统,主要提供两个功能:商品管理、会员管理。

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.写出三元运算的基本格式及作用?'''a if a>b else b''' # 2.什么是匿名函数? ...

  6. Vue小案例 之 商品管理------学习过滤器 使用过滤器处理日期的格式

    代码学习过滤器 过滤器介绍:过滤模型数据,在数据显示前做预处理操作: 内置过滤器:在1.x中,Vue提供了内置过滤器,但是在2.x中已经完全废除: 解决办法: (1)使用第三方库来替代1.x中的内置过 ...

  7. Vue小案例 之 商品管理------修改商品数量以及增加入库日期属性

    实现修改商品的数量: 加入的代码: css: .clear-btn{ text-align: right; padding-right: 10px; } .table-warp a{ text-dec ...

  8. Vue小案例 之 商品管理------批量删除与商品数量的调整

    通过索引进行删除,进行测试,是否获取其索引: 测试效果: 测试代码,在vue中定义一个空的数组,以便后面进行数据的绑定: data:{ imgUrl:'../res/images/', imgName ...

  9. Vue小案例 之 商品管理------删除商品与提示

    实现删除商品功能 根据索引来进行删除商品: 实现删除商品的HTML: <!--显示表格--> <div class="table-warp"> <di ...

随机推荐

  1. PHP XML To Array将XML转换为数组

    // Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误 function xml_to_array( $xml ) { $reg = "/<(\\w+)[^>]*?& ...

  2. C语言实现简化的正则表达式

    语法: 正则表达式和待匹配字符串都是一行 "^" 标记正则表达式的开始 "$" 标记正则表达式的结束 "*" 匹配前面的子表达式零次或多次 ...

  3. 微信小程序 上传图片

    效果图  如上,js  如下,在页面循环图片就可以 /** * 选择图片 */ uploadImgAdd: function(e) { var imgs = this.data.imgs; wx.ch ...

  4. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  5. 实验了一下对于struct引用的成员的改动

    今天写代码的时候,不确定struct用引用传递给函数的时候,他的成员在函数里面改变的时候,是否能影响到外面. 实验了一下 #include <stdio.h> #include <s ...

  6. C语言中static的使用

    在开发过程中.我们常常会须要定义一些static类型的变量或者函数.我们接下来来详细聊一下static: 1.修饰变量 当static来修饰一个变量时,就注定了这个变量的可见范围和生命周期: (1)当 ...

  7. Unity3d 开发(七)AssetBundle组织文件夹

    本文探讨怎样配置一个AssetBundle更为合理. 对于结构为 的文件夹结构,当中shared是Hero文件夹下须要用到的公用资源.即公有依赖.可採用例如以下的打包策略 整个文件夹打包 将整个100 ...

  8. Maven配置Spring+SpringMVC+MyBatis(3.2.2)Pom 以及 IntelliJ IDEA 怎样打开依赖视图

    Maven配置Spring+SpringMVC+MyBatis(3.2.2)Pom 配置原则: 利用依赖,将所需的jar包加载到project中. 先依赖主要jar包 Spring + Spring ...

  9. MYSQL源代码编译的变动

    Mysql的安装,对于mysql不同版本号的mysql源代码编译方式不一样 5.6.2的版本号開始编译方式已经由 configure 变成了cmake方式 ,相关的新的 编译方式在mysql官网已经提 ...

  10. HDU 5344(MZL&#39;s xor-(ai+aj)的异或和)

    MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...