asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T>
by 王继坤
at 2010-07-05 15:08:00
original http://www.cnblogs.com/wangjikun3/archive/2010/07/05/1771401.html
作者: 王继坤 发表于 2010-07-05 15:08 原文链接 阅读: 7 评论: 1
asp.net mvc 2 使用Linq to Sql 来操作数据库,基本实现面向对象编程,同样在操作数据是我们也可以使用面向对象的继承再加上泛型,是操作更加简单。代码简单,不详细说明,里面用到几个简单的扩展方法,以后会详细介绍。如db.find<T>(t),db.findkey<T>(id)。这是对dbcontent做了些扩展。
RepositoryController.cs 代码如下:
1 public class RepositoryController<T>: BaseController where T : class,new()
2 {
3 private Table<T> list()
4 {
5 return db.GetTable<T>();
6 }
7
8
9 public string orderby = "Id desc";
10 public virtual ActionResult Index(T art)
11 {
12 ViewData["searchModel"] = art;
13 var Model = db.Find<T>(art);
14 RecordCount = Model.Count();
15 Model=Model.OrderBy(orderby);
16
17 Model = Model.Skip((CurPage - 1) * PageSize).Take(PageSize);
18
19 GetPager();
20 return View("index", Model);
21 }
22 [ValidateInput(false)]
23 public virtual ActionResult Add()
24 {
25
26 return View(new T());
27 }
28 [HttpPost]
29 [ValidateInput(false)]
30 public virtual ActionResult Add(T Model)
31 {
32 if (ModelState.IsValid)
33 {
34 try
35 {
36 list().InsertOnSubmit(Model);
37 db.SubmitChanges();
38 return RedirectToAction("index");
39 }
40 catch
41 {
42 return View(Model);
43 }
44 }
45 else
46 return View(Model);
47 }
48 [ValidateInput(false)]
49 public virtual ActionResult Edit(int id)
50 {
51 return View(db.FindKey<T>(id));
52 }
53 [HttpPost]
54 [ValidateInput(false)]
55 public virtual ActionResult Edit(int id, T Model)
56 {
57 try
58 {
59 var cate = db.FindKey<T>(id);
60 UpdateModel(cate);
61 db.SubmitChanges();
62 return RedirectToAction("index");
63 }
64 catch
65 {
66 return View(Model);
67 }
68 }
69 public virtual ActionResult Delete(int id)
70 {
71 list().DeleteOnSubmit(db.FindKey<T>(id));
72 db.SubmitChanges();
73 return RedirectToAction("index");
74 }
75
76
77 }
2 {
3 private Table<T> list()
4 {
5 return db.GetTable<T>();
6 }
7
8
9 public string orderby = "Id desc";
10 public virtual ActionResult Index(T art)
11 {
12 ViewData["searchModel"] = art;
13 var Model = db.Find<T>(art);
14 RecordCount = Model.Count();
15 Model=Model.OrderBy(orderby);
16
17 Model = Model.Skip((CurPage - 1) * PageSize).Take(PageSize);
18
19 GetPager();
20 return View("index", Model);
21 }
22 [ValidateInput(false)]
23 public virtual ActionResult Add()
24 {
25
26 return View(new T());
27 }
28 [HttpPost]
29 [ValidateInput(false)]
30 public virtual ActionResult Add(T Model)
31 {
32 if (ModelState.IsValid)
33 {
34 try
35 {
36 list().InsertOnSubmit(Model);
37 db.SubmitChanges();
38 return RedirectToAction("index");
39 }
40 catch
41 {
42 return View(Model);
43 }
44 }
45 else
46 return View(Model);
47 }
48 [ValidateInput(false)]
49 public virtual ActionResult Edit(int id)
50 {
51 return View(db.FindKey<T>(id));
52 }
53 [HttpPost]
54 [ValidateInput(false)]
55 public virtual ActionResult Edit(int id, T Model)
56 {
57 try
58 {
59 var cate = db.FindKey<T>(id);
60 UpdateModel(cate);
61 db.SubmitChanges();
62 return RedirectToAction("index");
63 }
64 catch
65 {
66 return View(Model);
67 }
68 }
69 public virtual ActionResult Delete(int id)
70 {
71 list().DeleteOnSubmit(db.FindKey<T>(id));
72 db.SubmitChanges();
73 return RedirectToAction("index");
74 }
75
76
77 }
最新新闻:
· 商业周刊:纽约时报应向谷歌和读者收费(2010-07-05 15:10)
· 维基百科因数据中心断电全球宕机(2010-07-05 15:09)
· 赖霖枫:雨林木风5年时间完成上市(2010-07-05 14:55)
· 门户微博研究报告新浪遥遥领先 搜狐和网易掉队(2010-07-05 14:50)
· Linux中的特种兵 十八个特别发行版(2010-07-05 14:30)
编辑推荐:C#大论战:在讨论中学习