MVC对数据库增删改查,使用实体模型
2017-08-03 18:31
323 查看
使用了MVC之后,对数据库的操作变得简单多了。创建MVC后,在model层添加一个ADO.NET实体数据模型。对应于我们要操作的数据库的表。
在控制器编写如下代码,注意一个控制器应对应于一个视图,如果一个视图对应于两个控制器,那么在行为前加上[HttpPost],那么此行为为post时执行。默认为GET。
DbContext为上下文对象,很重要的一个要记住的类。我们新建的实体数据模型名为 Database1Entities;
public ActionResult Index()
{
DbContext db = new Database1Entities();//子类赋值给父类
IQueryable<Table> t1 = db.Set<Table>();//将所有数据传入
return View(t1);//返回到VIEW中
}
//控制器所有代码如下
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using crud.Models;
using System.Data.Entity.Migrations;
using System.Web.Routing;
namespace crud.Controllers
{
public class CRUDController : Controller
{
//
// GET: /CRUD/
public ActionResult Index()
{
DbContext db = new Database1Entities();
IQueryable<Table> t1 = db.Set<Table>();
return View(t1);
}
public ActionResult create()
{
return View();
}
[HttpPost]
public ActionResult create(Table t1)
{
DbContext db = new Database1Entities();
db.Set<Table>().Add(t1);
int res= db.SaveChanges();
if (res>0)
{
return Redirect(@Url.Action("index","crud"));
}
else
{
return Redirect(@Url.Action("create","crud"));
}
}
public ActionResult delete(int Id)
{
DbContext db = new Database1Entities();
var deleteVal = db.Set<Table>().Where(u => u.Id == Id).FirstOrDefault();
db.Set<Table>().Remove(deleteVal);
int res=db.SaveChanges();
if (res>0 )
{
return Redirect(@Url.Action("INDEX","CRUD"));
}
else
{
return Redirect(@Url.Action("", ""));
}
}
public ActionResult change(int Id)
{
DbContext db = new Database1Entities();
ViewData.Model=db.Set<Table>().Where(u=>u.Id==Id).FirstOrDefault();
//IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == Id);
return View();
}
[HttpPost]
public ActionResult change(Table table)
{
DbContext db = new Database1Entities();
IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == table.Id);
db.Set<Table>().AddOrUpdate(table);
db.SaveChanges();
return Redirect(@Url.Action("index","CRUD"));
}
}
}
//VIEW代码如下
@using crud.Models
@model IQueryable< crud.Models.Table>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.ActionLink("Add", "create", "CRUD");
<br />
<table border="1">
<tr>
<th>id</th>
<th>username</th>
<th>delete</th>
</tr>
@foreach (Table table in Model)
{
<tr>
<td>@table.Id</td>
<td>@table.username</td>
<td>@Html.ActionLink("delete", "delete", "CRUD", new RouteValueDictionary(new {Id=@table.Id}),null)</td>
<td>@Html.ActionLink("change","change","CRUD",new RouteValueDictionary(new {Id= @table.Id}),null)</td>
</tr>
}
</table>
</div>
</body>
</html>
//创建代码如下
@model crud.Models.Table
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>create</title>
</head>
<body>
<div>
a370
@using (Html.BeginForm("create", "crud", FormMethod.Post))
{
<span>username</span>
@Html.TextBoxFor(u => u.username)
<br />
<input type="submit" value="Click" />
}
</div>
</body>
</html>
//修改代码如下
@model crud.Models.Table
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>change</title>
</head>
<body>
<div>
@using (Html.BeginForm("change", "crud", FormMethod.Post))
{
@Model.Id
@Html.HiddenFor(n => n.Id)
@Html.TextBoxFor(n=>n.username)
<input type="submit" value="change" />
}
</div>
</body>
</html>
完成
在控制器编写如下代码,注意一个控制器应对应于一个视图,如果一个视图对应于两个控制器,那么在行为前加上[HttpPost],那么此行为为post时执行。默认为GET。
DbContext为上下文对象,很重要的一个要记住的类。我们新建的实体数据模型名为 Database1Entities;
public ActionResult Index()
{
DbContext db = new Database1Entities();//子类赋值给父类
IQueryable<Table> t1 = db.Set<Table>();//将所有数据传入
return View(t1);//返回到VIEW中
}
//控制器所有代码如下
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using crud.Models;
using System.Data.Entity.Migrations;
using System.Web.Routing;
namespace crud.Controllers
{
public class CRUDController : Controller
{
//
// GET: /CRUD/
public ActionResult Index()
{
DbContext db = new Database1Entities();
IQueryable<Table> t1 = db.Set<Table>();
return View(t1);
}
public ActionResult create()
{
return View();
}
[HttpPost]
public ActionResult create(Table t1)
{
DbContext db = new Database1Entities();
db.Set<Table>().Add(t1);
int res= db.SaveChanges();
if (res>0)
{
return Redirect(@Url.Action("index","crud"));
}
else
{
return Redirect(@Url.Action("create","crud"));
}
}
public ActionResult delete(int Id)
{
DbContext db = new Database1Entities();
var deleteVal = db.Set<Table>().Where(u => u.Id == Id).FirstOrDefault();
db.Set<Table>().Remove(deleteVal);
int res=db.SaveChanges();
if (res>0 )
{
return Redirect(@Url.Action("INDEX","CRUD"));
}
else
{
return Redirect(@Url.Action("", ""));
}
}
public ActionResult change(int Id)
{
DbContext db = new Database1Entities();
ViewData.Model=db.Set<Table>().Where(u=>u.Id==Id).FirstOrDefault();
//IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == Id);
return View();
}
[HttpPost]
public ActionResult change(Table table)
{
DbContext db = new Database1Entities();
IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == table.Id);
db.Set<Table>().AddOrUpdate(table);
db.SaveChanges();
return Redirect(@Url.Action("index","CRUD"));
}
}
}
//VIEW代码如下
@using crud.Models
@model IQueryable< crud.Models.Table>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.ActionLink("Add", "create", "CRUD");
<br />
<table border="1">
<tr>
<th>id</th>
<th>username</th>
<th>delete</th>
</tr>
@foreach (Table table in Model)
{
<tr>
<td>@table.Id</td>
<td>@table.username</td>
<td>@Html.ActionLink("delete", "delete", "CRUD", new RouteValueDictionary(new {Id=@table.Id}),null)</td>
<td>@Html.ActionLink("change","change","CRUD",new RouteValueDictionary(new {Id= @table.Id}),null)</td>
</tr>
}
</table>
</div>
</body>
</html>
//创建代码如下
@model crud.Models.Table
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>create</title>
</head>
<body>
<div>
a370
@using (Html.BeginForm("create", "crud", FormMethod.Post))
{
<span>username</span>
@Html.TextBoxFor(u => u.username)
<br />
<input type="submit" value="Click" />
}
</div>
</body>
</html>
//修改代码如下
@model crud.Models.Table
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>change</title>
</head>
<body>
<div>
@using (Html.BeginForm("change", "crud", FormMethod.Post))
{
@Model.Id
@Html.HiddenFor(n => n.Id)
@Html.TextBoxFor(n=>n.username)
<input type="submit" value="change" />
}
</div>
</body>
</html>
完成
相关文章推荐
- EF简单实用,在MVC里面使用EF,根据空模型,添加实体,生成数据库
- Android GreenDao使用总结(包括模型生成、增删改查、修改存储路径、数据库更新升级和加解密数据库)
- ASP.NET MVC 教程 - 使用实体框架创建模型类(C#)
- ThinkPHP 的模型使用对数据库增删改查(五)
- ThinkPHP 的模型使用对数据库增删改查(五)
- ASP.NET MVC 4用ADO.NET实体数据模型连接数据库
- ASP.NET中实体数据库模型的增删改查的用法。
- EF实体框架模型使用Linq获取数据库数据
- struts MVC模型删除数据库
- 使用ASP.NET MVC(C#)15分钟内创建一个电影数据库程序
- 使用数据集和数据适配器对数据库增删改查
- vs2010 下的ado.net 实体模型创建数据库时datetime 类型的问题
- ASP.NET MVC中使用View Model分离领域模型
- 初次使用T4引擎生成数据库表实体
- Asp.Net MVC 模型(使用LINQ to SQL创建Model类) – Part.2
- ADO.NET Entity Framework如何:使用实体数据模型向导(实体框架)
- 使用T4为数据库自动生成实体类(C#)
- 使用LINQS 0.0.6描述数据库模型和创建数据库
- C#2005中使用控件DataGridView实现对数据库增删改查操作