EF4.4增删改查实例
第一、先创建一个名为Store数据库,将下面脚本代码执行创建表;
USE [Store]
GO /****** Object: Table [dbo].[Category] Script Date: 03/25/2014 09:39:23 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Category](
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO /****** Object: Table [dbo].[Product] Script Date: 03/25/2014 09:39:31 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[CategoryID] [int] NULL,
[UnitPrice] [decimal](18, 2) NULL,
[UnitsInStock] [int] NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO 第二、控制器ProductController
//
// GET: /Product/ public ActionResult Index()
{
var products = db.Products;
return View(products.ToList());
} /// <summary>
// GET: /Products/Details/5
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Details(int id = )
{
var product = db.Products.First(p => p.ID == id);
if (product == null)
return HttpNotFound();
return View(product);
} //
// GET: /Products/Create
public ActionResult Create()
{
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name");
return View();
} //
// POST: /Products/Create [HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.AddToProducts(product);
db.SaveChanges();
return RedirectToAction("Index");
} ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
return View(product);
} //
//GET: /Products/Edit/5
public ActionResult Edit(int id = )
{
Product entity = db.Products.First(p => p.ID == id);
if (entity == null)
return HttpNotFound();
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", entity.CategoryID);
return View(entity);
} //
//POST: /Products/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", product.CategoryID);
return View(product);
} //
// GET: /Products/Delete/5 public ActionResult Delete(int id = )
{
Product product = db.Products.First(p => p.ID == id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
} //
// POST: /Products/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Product product = db.Products.First(p => p.ID == id);
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Deleted);
db.SaveChanges();
return RedirectToAction("Index");
} 第3、视图
视图结构:
下面是视图代码:
Index:
@model IEnumerable<TMVC.DAL.Product>
@{
ViewBag.Title = "Index";
}
<h2>
产品页面</h2>
<p>@Html.ActionLink("添加", "Create")</p>
<table>
<tr>
@*<th>@Html.DisplayNameFor(model => model.Name)
</th>
<th>@Html.DisplayNameFor(model => model.CategoryID)
</th>
<th>@Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th>@Html.DisplayNameFor(model => model.UnitsInStock)
</th>*@
<th>
名称
</th>
<th>
类别
</th>
<th>
价格
</th>
<th>
库存
</th>
<th>
编辑
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(mi => item.Name)
</td>
<td>@Html.DisplayFor(mi => item.CategoryID)
</td>
<td>@Html.DisplayFor(mi => item.UnitPrice)
</td>
<td>@Html.DisplayFor(mi => item.UnitsInStock)
</td>
<td>@Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
@Html.ActionLink("详细", "Details", new { id = item.ID }) | @Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
Create:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Create";
}
<h2>
添加</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>产品</legend>
<div class="editor-label">
名称 @* @Html.LabelFor(model => model.Name)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别 @* @Html.LabelFor(model => model.CategoryID, "CategoryPCategor")*@
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格 @*@Html.LabelFor(model => model.UnitPrice)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存@* @Html.LabelFor(model => model.UnitsInStock)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="确定添加" />
</p>
</fieldset>
}
Delete:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Delete";
}
<h2>
删除</h2>
<h3>
你确定要删除这个产品吗?</h3>
<fieldset>
<legend>产品</legend>
<div class="display-label">
名称
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
类别
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
价格
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitPrice)
</div>
<div class="display-label">
库存
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitsInStock)
</div>
</fieldset>
@using (Html.BeginForm())
{
<p>
<input type="submit" value="删除" />
|
@Html.ActionLink("返回产品列表", "Index")
</p>
}
Details:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Details";
}
<h2>
详细</h2>
<fieldset>
<legend>产品</legend>
<p>
名称:@Html.DisplayFor(model => model.Name)</p>
<p>
类别:@Html.DisplayFor(model => model.CategoryID)</p>
<p>
价格:@Html.DisplayFor(model => model.UnitPrice)</p>
<p>
库存:@Html.DisplayFor(model => model.UnitsInStock)</p>
</fieldset>
<p>@Html.ActionLink("编辑", "Edit", new { id = Model.ID }) | @Html.ActionLink("返回", "Index")</p>
Edit:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Edit";
}
<h2>
编辑</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true) <fieldset>
<legend>产品</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
名称
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="保存" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("返回产品列表", "Index")
</div>
RouteConfig
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
EF4.4增删改查实例的更多相关文章
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)
1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- yii2.0增删改查实例讲解
yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...
- 百度鹰眼Java接口调用增删改查实例
因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...
- 【php增删改查实例】第四节 -自己 DIY 一个数据库管理工具
本节介绍如何自己DIY一个数据库管理工具,可以在页面输入sql 进行简单的增删改查操作. 首先,找到xampp的安装目录,打开htdocs: 新建一个php文件,名称为 mysqladmin.php ...
- Maven多模块项目+MVC框架+AJAX技术+layui分页对数据库增删改查实例
昨天刚入门Maven多模块项目,所以简单写了一个小测试,就是对数据库单表的增删改查,例子比较综合,写得哪里不妥还望大神赐教,感谢! 首先看一下项目结构: 可以看到,一个项目MavenEmployee里 ...
- 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作
PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...
- 【php增删改查实例】第十节 - 部门管理模块(新增功能)
正常情况下,在一个部门管理页面,不仅仅需要展示列表数据,还需要基本的增删改操作,所以,我们先把之前写好的新增功能集成进来. 在toolbar中,添加一个新增按钮. <div id="t ...
- Java MVC 增删改查 实例
需求:实现增加新部门的功能,对应数据库表示Oracle的dept表 一.Java MVC 增 实现: 1.视图层(V):注册部门 deptAdd.jsp 在注册新部门页面只需输入“部门名称”和“城市” ...
随机推荐
- windows通过命令方式解压zip文件
1.需要下载unzip 地址:http://gnuwin32.sourceforge.net/packages/unzip.htm 下载exe版本 2.安装后将bin目录下的unzip.exe文件放在 ...
- .net core 使用 redis
.net core 使用 redis 个人感觉.net core 对于微软技术而言有很重要的意义 ,所以最近已有时间就想看一看关于.net core 的文章. 今天我就来写一写如何在.net core ...
- 使用CDI+制作支持半透明的Panle
创建一个自定义控件程序集,并修改父类为Panle,添加如下代码: public partial class OpaqueLayer : Panel { private Color transparen ...
- .netcore Swagger 生成 api接口文档
1, 引用第三方包, Swashbuckle.AspNetCore Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerUI 最简 ...
- ES6——内置对象的扩展
字符串的扩展 //模版字符串 let flag=true; let hml=`<ul> <li> <span></span> <span>& ...
- asp.net mvc 3 linq实现数据的增、删、改、查、
添加数据 定义一个对象: public class Student { public int id{get; set;} public string Name{get;set;} public str ...
- iOS 禁止多按钮同时响应
只需要对相应的按钮添加一行代码 [aButton setExclusiveTouch:YES];
- Mybatis 类的转换器
想一个事情 ,例: 我数据库的表 定义了一个表student 里面有一个字段 stu_sex int类型 可是我对应的实体类是 String 类型或者其他类型 这个时候 实体类与数据库表肯定对应不 ...
- [javascript]——移动端 HTML5 图片上传预览和压缩
在开发移动端web网页中,我们不可避免的会遇到文件上传的功能,但由于手机图片尺寸太大,上传时间过长导致用户体验太差,就需要在上传前对图片进行一定的压缩. 在代码之前,有必要先了解我们即将使用到的几个A ...
- DataList用法总结
设计模版: 页眉<HeaderTemplate> </HeaderTemplate> 页脚<FooterTemplate> </FooterTemplat ...
