新的方法(Set<T>)实现mvc的crud
model层的属性为:
public partial class UserInfo
{
public int Uid { get; set; }
public string UName { get; set; }
}
数据上下文类:生成数据
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure; public partial class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
} public DbSet<UserInfo> UserInfo { get; set; } }
controller层: crud是在集合的基础上完成的(实则对集合的crud)
public class UserInfoCrudController : Controller
{
DbContext dbContext = new MyContext(); //由父类指向子类 实例化生成数据库 Set<>方法产生的是一个集合
// GET: /UserInfoCrud/ public ActionResult Index()
{
var list = dbContext.Set<UserInfo>(); return View(list);
} public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(UserInfo userInfo)
{
dbContext.Set<UserInfo>().Add(userInfo);
int result = dbContext.SaveChanges();//如果内存中的数据发生了变化,并且希望将这个变化映射到数据库,需要执行这个方法
if (result > )
{
return Redirect(@Url.Action("Index", "UserInfoCrud"));
}
else
{
return Redirect(@Url.Action("Add"));
}
} public ActionResult Edit(int id)
{
ViewData.Model = dbContext.Set<UserInfo>()
.Where(u => u.Uid == id).FirstOrDefault(); return View();
}
[HttpPost]
public ActionResult Edit(UserInfo userInfo)
{
dbContext.Set<UserInfo>().AddOrUpdate(userInfo);
int result = dbContext.SaveChanges();
if (result > )
{
return Redirect(Url.Action("Index"));
}
else
{
return Redirect(Url.Action("Edit", new RouteValueDictionary(new
{
id = userInfo.Uid
})));
} } public ActionResult Remove(int id)
{
var userInfo = dbContext.Set<UserInfo>()
.Where(u => u.Uid == id)
.FirstOrDefault();
dbContext.Set<UserInfo>().Remove(userInfo);
dbContext.SaveChanges(); return Redirect(Url.Action("Index"));
}
}
views层:
(1)数据展示部分
@using model所在 的命名空间
@model IQueryable<t2_EFTest.Models.UserInfo>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.ActionLink("添加","Add","UserInfoCrud")
<hr/>
<table border="">
<tr>
<th>编号</th>
<th>姓名</th>
<th>修改</th>
<th>删除</th>
</tr>
@foreach (UserInfo userInfo in Model)
{
<tr>
<td>@userInfo.Uid</td>
<td>@userInfo.UName</td>
<td>
@Html.ActionLink("修改","Edit","UserInfoCrud",
new RouteValueDictionary(new {id=@userInfo.Uid}),null)
</td>
<td>
@Html.ActionLink("删除",
"Remove",
"UserInfoCrud",
new RouteValueDictionary(new
{
id=@userInfo.Uid
}),null)
</td>
</tr>
}
</table>
</div>
</body>
</html>
(2)数据添加部分
@model t2_EFTest.Models.UserInfo
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Add</title>
</head>
<body>
<div>
@using (Html.BeginForm("Add", "UserInfoCrud", FormMethod.Post))
{
<span>姓名:</span>
@Html.TextBoxFor(u=>u.UName)
<br/>
<input type="submit" value="添加"/>
}
</div>
</body>
</html>
(3)数据修改部分
@model t2_EFTest.Models.UserInfo
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<div>
@using (Html.BeginForm("Edit", "UserInfoCrud", FormMethod.Post))
{
<span>编号:</span>
@Model.Uid
@Html.HiddenFor(u=>u.Uid)
<br/>
<span>姓名:</span>
@Html.TextBoxFor(u=>u.UName)
<br/>
<input type="submit" value="修改"/>
}
</div>
</body>
</html>
新的方法(Set<T>)实现mvc的crud的更多相关文章
- PHP面向对象_重载新的方法(parent::)
在学习PHP 这种语言中你会发现, PHP中的方法是不能重载的, 所谓的方法重载就是定义相同的方法名,通过“参数的个数“不同或“参数的类型“不 同,来访问我们的相同方法名的不同方法.但是因为PHP是弱 ...
- IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误
IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...
- 【PHP面向对象(OOP)编程入门教程】12.重载新的方法(parent::)
在学习PHP 这种语言中你会发现, PHP中的方法是不能重载的, 所谓的方法重载就是定义相同的方法名,通过“参数的个数“不同或“参数的类型“不 同,来访问我们的相同方法名的不同方法.但是因为PHP是弱 ...
- js页面跳转 和 js打开新窗口 方法
js页面跳转 和 js打开新窗口 方法 第一种: 第二种: 第三种: 第四种: 第五种: 1.在原来的窗体中直接跳转用 window.location.href="你所要跳转的页面" ...
- 新的方法 (New Approach)¶
第一章:简介 - ANSI Common Lisp 中文版 新的方法 (New Approach)¶ 本书的目标之一是不仅是教授 Lisp 语言,而是教授一种新的编程方法,这种方法因为有了 Lisp ...
- Spring aop:decare-parent 为类增加新的方法
Spring aop:decare-parent 为类增加新的方法: 使用XML配置的方式: XML: <?xml version="1.0" encoding=" ...
- javascript插入before(),after()新DOM方法
随着web的技术突飞猛进的发展.HTML5 ES6等新技术的发展,与此同时DOM等标准也在悄悄的进步,各大浏览器也在悄悄的发展适配新的属性和方法,今天我们来看看Javascript新的DOM的方法 二 ...
- Java 8新特性:新语法方法引用和Lambda表达式及全新的Stream API
新语法 方法引用Method references Lambda语法 Lambda语法在AndroidStudio中报错 Stream API 我正参加2016CSDN博客之星的比赛 希望您能投下宝贵 ...
- JAVA8新特性——方法引用
JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ 在Lamda新特性的支持下,JAVA8中可以使用lamda表达式来创建匿名方法.然而,有时候我们仅仅是需要调用一个已存在的方法(如 ...
- win2012 挂载硬盘即增加新硬盘方法
这篇文章主要介绍了win2012 挂载硬盘即增加新硬盘方法,需要的朋友可以参考下 点击左下角的服务器管理图标 点击右上角的“工具”,再选择“计算机管理” 再点击“磁盘管理” 在磁盘1的按钮处单击右键, ...
随机推荐
- 雷林鹏分享:Ruby CGI Sessions
Ruby CGI Sessions CGI::Session 可以为用户和CGI环境保存持久的会话状态,会话使用后需要关闭,这样可以保证数据写入到存储当中,当会话完成后,你需要删除该数据. #!/us ...
- C++设计模式之职责链模式
代码实现: // chainResbonsibility.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stri ...
- 程序级的AOP到底好不好?
很多年前模拟过Spring的AOP机制,简单的实现其实不难,但真正要保证切入代码符合预期的设计,不会引起负面影响,特别是要保证原来逻辑的稳定性,即AOP的强壮性.个人感觉还是很难,如果横切的代码过多, ...
- windows下 两个版本的JDK环境变量进行切换 MARK
我们平时在window上做开发的时候,可能需要同时开发两个甚至多个项目,有时不同的项目对JDK的版本要求有区别,为了简化操作,我们可以通过批处理文件来完成环境变量切换的任务.使用方法:阅读代码我们就会 ...
- PHP write byte array to file
/********************************************************************************* * PHP write byte ...
- Jmeter-Critical Section Controller(临界区控制器)
The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will ...
- LTE-Advanced(4G)主要技术学习:CA、CoMp、HetNet
CA:Carrier Aggregation,载波聚合 从LTE到LTE-Advanced演进过程中,更宽频谱的需求是影响演进的最重要因素,为此3GPP标准提出了载波聚合技术.简单地说,它可以将多个载 ...
- ambassador 学习六 Module说明
模块允许给与特定的mapping 或者整体添加特定的行为,方便进行系统的控制. 当前的module 定义主要是系统级别的 当前系统主要的配置 --- apiVersion: ambassador/v0 ...
- 使用IAR编译STM8S 怎样生产烧录文件
IAR编译后能够生成的烧录文件格式有4中,例如以下 第一种是Motorola,其生成文件和STVD生成烧录文件.s19格式一样的,即能够通用 另外一种是16进制,keil等等常都用到的. 第三种是 ...
- Dictionary字典类介绍
说明 必须包含名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而值不 ...