Basic ASP. NET mvc2.0 tutorial for implementing crud operations

Source: Internet
Author: User
Tags allkeys visual studio 2010

This demo project implements crud addition, deletion, query, and modification operations.

Development Environment: Visual Studio 2010 + LINQ to SQL + SQL Server 2005

I. Demo

1, download the demo source code: http://files.cnblogs.com/rubyloveromantic/MyMikeDemo.rar

2. in Visual Studio 2010, open the demo project and run it.

2. Create this demo project from scratch

1. Add the database in the downloaded demo source code: mikew.mvp. MDF [OK]
2. Create an MVC preview ApplicationProgramIn the models directory, add a LINQ to SQL file: mikew.mvp. dbml, and drag the user table in the database into the linqtosql design window. [OK]
3. Add a namespace in Web. config: [OK]
4. Create a view file in the relevant directory [OK]


5. Create a userscontroller. CS file under the controllers/users/directory, and enter the ADD, query, modify, and code. [OK]

 

Userscontrol

   Public     Class  Userscontroller: Controller
{
Mikew.mvpdatacontext DB = New Mikeappsmvpdatacontext ();

# Region CRUD operation
Public Actionresult list ()
{
List < User > Model = DB. User. tolist ();
Return View (model );
}

Public Actionresult create ()
{
Return View ();
}

[Httppost]
Public Actionresult add ()
{
User Model = New User ();
Model. userid = 0 ;
Updatemodel (model, request. Form. allkeys );
DB. User. insertonsubmit (model );
DB. submitchanges ();

Return Redirecttoaction ( " List " );
}
Public Actionresult details ( Int Userid)
{
User Model = DB. User. firstordefault (E => E. userid = Userid );
Return View (model );
}
Public Actionresult edit ( Int Userid)
{
User Model = DB. User. firstordefault (E => E. userid = Userid );
Return View (model );
}

[Httppost]
Public Actionresult edit ( Int Userid, String Parm)
{
If (Parm = " Update " )
{
User Model = DB. User. firstordefault (E => E. userid = Userid );
Updatemodel (model, request. Form. allkeys );
DB. submitchanges ();
}
Return Redirecttoaction ( " List " );
}
Public Actionresult Delete ( Int Userid)
{

User Model = DB. User. firstordefault (E => E. userid = Userid );
DB. User. deleteonsubmit (model );
DB. submitchanges ();

Return Redirecttoaction ( " List " );
}
# Endregion

# Region Data Reset

Public Actionresult datareset ()
{
// Batch Delete
List < User > Oldlist = DB. User. tolist ();
DB. User. deleteallonsubmit (oldlist );

// Batch add
List < User > Newlist = New List < User >
{
New User {Username = " Sun yat-sen " , Age = 53 , Career = " Chinese president " },
New User {Username = " Jiang zhongzheng " , Age = 36 , Career = " Chinese president " },
New User {Username = " Bush " , Age = 61 , Career = " US President " },
New User {Username = " Lenin " , Age = 49 , Career = " Evil Soviet Union " },
New User {Username = " Stalin " , Age = 49 , Career = " Evil Soviet Union " },
New User {Username = " Bill Gates " , Age = 62 , Career = " Microsoft boss " },
New User {Username = " Scott Guthrie " , Age = 33 , Career = " ASP. NET leader " }
};
DB. User. insertallonsubmit (newlist );
DB. submitchanges ();
Return Redirecttoaction ( " List " );
}
# Endregion
}

Note: This course is intended for Mike in Jesus.CodeTo mvc2.0 ). No infringement.

Related Article

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.