ASP.NET Mvc Preview 5 演示Demo #1 实现CRUD增删查改
MVC演示Demo系列: http://mike108mvp.cnblogs.com
ASP.NET MVC QQ交流群 1215279 欢迎对 ASP.NET MVC 感兴趣的朋友加入
这个演示项目实现了CRUD增删查改操作。
开发环境:Visual Studio 2008 SP1 + ASP.NET MVC Preview 5 + Linq To Sql + SQL Server 2005
ASP.NET MVC Preview 5 下载地址: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16775
一、Demo演示
1、下载本演示Demo源码:https://files.cnblogs.com/mike108mvp/MvcP5_Mike108_Demo01.rar
2、附加下载Demo源码中的数据库:mike108mvp.mdf 【ok】
3、Visual Studio 2008打开本演示项目,运行即可。【ok】
二、从头开始建立这个演示项目
1、附加下载的Demo源码中的数据库:mike108mvp.mdf 【ok】
2、新建一个MVC Preview5应用程序,在Models目录下,添加一个Linq To Sql文件:Mike108mvp.dbml,将数据库中的User表拖入LinqToSql设计窗口中。【ok】
3、web.config中添加命名空间:【ok】
<add namespace="MvcP5_Mike108_Demo01.Models"/>
<add namespace="MvcP5_Mike108_Demo01.Controllers"/>
4、Views/Users/Edit.aspx.cs 将 public partial class Edit : ViewPage 修改为 public partial class Edit : ViewPage<User> 【ok】
5、Views/Users/Detail.aspx.cs 将 public partial class Detail : ViewPage 修改为 public partial class Detail : ViewPage<User> 【ok】
6、Controllers/Users/目录下,新建一个UsersController.cs文件,输入增删查改代码。【ok】
public class UsersController : Controller
{
Mike108mvpDataContext db = new Mike108mvpDataContext();
CRUD操作#region CRUD操作
public ActionResult UserAdd()
{
return View(@"Add");
}
public ActionResult UserCreate()
{
if (Request.HttpMethod != "POST")
{ return View(@"Add"); }
User model = new User();
UpdateModel(model, Request.Form.AllKeys);
db.Users.InsertOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserEdit(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Edit", model);
}
public ActionResult UserUpdate(int userId)
{
if (Request.HttpMethod != "POST")
{ return RedirectToAction("UserEdit", new { userId = userId }); }
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
UpdateModel(model, Request.Form.AllKeys);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserDetail(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Detail", model);
}
public ActionResult UserDelete(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
db.Users.DeleteOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserList()
{
ViewData["UserList"] = db.Users.ToList();
return View(@"List");
}
#endregion
数据重置#region 数据重置
public ActionResult DataReset()
{
//批量删除
List<User> oldList = db.Users.ToList();
db.Users.DeleteAllOnSubmit(oldList);
//批量添加
List<User> newlist = new List<User>
{
new User { UserName = "孙中山", Age = 53, Career = "中国总统" },
new User { UserName = "蒋中正", Age = 36, Career = "中国总统" },
new User { UserName = "小布什", Age = 61, Career = "美国总统" },
new User { UserName = "列宁", Age = 49, Career = "邪恶苏联" },
new User { UserName = "斯大林", Age = 49, Career = "邪恶苏联" },
new User { UserName = "比尔盖茨", Age = 63, Career = "微软老大" }
};
db.Users.InsertAllOnSubmit(newlist);
db.SubmitChanges();
return RedirectToAction("UserList");
}
#endregion
}
下载本演示Demo源码:https://files.cnblogs.com/mike108mvp/MvcP5_Mike108_Demo01.rar