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.