Asp.net MVC 2 uses LINQ to SQL to operate databases and basically implement object-oriented programming. Similarly, when operating data, we can also use object-oriented inheritance with generics, which makes operations easier.CodeSimple, not described in detail, it uses a few simple extension methods, will be described in detail later. Such as DB. Find <t> (T) and DB. findkey <t> (ID ). This is an extension of dbcontent.
The repositorycontroller. CS code is as follows:
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 }