惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
V
Visual Studio Blog
有赞技术团队
有赞技术团队
U
Unit 42
D
Docker
小众软件
小众软件
F
Full Disclosure
I
Intezer
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
B
Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
S
Security Affairs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
量子位
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Heimdal Security Blog
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
S
Schneier on Security

博客园 - 王继坤

DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现 asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题 asp.net mvc 2 简简单单做开发 自定义Controller基类 - 王继坤 asp.net mvc 2 简简单单做开发 自定义DropdownList控件 asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面 asp.net mvc 2 DisplayTemplates 的使用 FcDigg 源码 asp.net 3.5 linq to sql 扩展方法 linq to access 简单实现 实例demo linq to access 简单实现 asp.net mvc 随想 asp.net mvc ajax实现1 asp.net ajax fccms 小型简单个人blog源码 FCKEDITOR中文使用说明 js调用 dotfuscator.exe 采用线程生成静态页面 新的开始--- gridview 实现多字段综合查询
asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T>
王继坤 · 2010-07-05 · via 博客园 - 王继坤

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     }