Asp.net MVC 2 simple and simple development to achieve basic data operations repositorycontroller

Source: Internet
Author: User

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 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.