No nonsense MVC getting started tutorial 10 [practice 2: user management]

Source: Internet
Author: User
Tags actionlink
I. Objectives of this Article

Learn how to create MVC management functions

Ii. Contents of this Article

1. Query

2. Modify

3. Delete

4.CodeDownload

1. Query

1) view code:

 1 @ Model pagedlist <  Mvc3  . Demomodel. User  >  2   @ Using webdiyer. webcontrols. MVC;  3   @ Using (html. beginform ("Main", "manage", formmethod. Get ))  4   { 5       <  Span  > User name: </  Span  >  6   @ Html. Textbox ("username", viewdata ["username"])  7       <  Input  Type  = "Submit"  Value  = "Query"  />  8   }  9   @ Foreach (mvc3.demomodel. User user in Model)  10   {  11 @ User. userid <  Span  > --- </  Span  > @ User. Username <  Span > --- </  Span  >   12 @ Html. actionlink ("modify", "useredit", new {id = user. userid }) <  Span  > --- </  Span  >   13 @ Html. actionlink ("details", "userdetail", new {id = user. userid }) <  Span > --- </  Span  >   14 @ Html. actionlink ("delete", "userremove", new {id = user. userid }) <  Span  > --- </  Span  >   15   16       <  BR  /> 17   }  18   <  BR  />  19   <  BR  />  20   @ Html. Pager (model, new pageroptions  21   {  22   Pageindexparametername = "ID ",  23  Showpageindexbox = true,  24   Firstpagetext = "Homepage ",  25   Prevpagetext = "Previous Page ",  26   Nextpagetext = "next page ",  27   Lastpagetext = "last page ",  28   Pageindexboxtype = pageindexboxtype. Textbox,  29   Pageindexboxwrapperformatstring = "Enter the number of pages {0 }",  30  Gobuttontext = "go"  31   })  32   <  BR  />  33 > A total of @ model. totalitemcount comments @ model. currentpageindex/@ model. totalpagecount are displayed on the page.

2) control code:

 1           Public Actionresult main ( Int ? Id = 1  )  2  {  3 Viewdata [ "  Username  " ] = String  . Empty;  4               If (Request. querystring [ "  Username  " ]! = Null  )  5   { 6 Viewdata [ "  Username  " ] = Request. querystring [ "  Username  "  ]. Tostring ();  7   }  8 List <model. User> userlist = New List <model. User> ();  9              Int Totalcount = 0  ;  10               Int Pageindex = ID ?? 1  ;  11 Userlist = demorepository. User. getlist (viewdata [ "  Username  " ]. Tostring (), 2 , (Pageindex- 1 )* 2 ,Out  Totalcount );  12 Pagedlist <model. User> mpage = userlist. asqueryable (). topagedlist ( 0 , 2  );  13 Mpage. totalitemcount = Totalcount;  14 Mpage. currentpageindex = ( Int ) (ID ?? 1  );  15              Return  View (mpage );  16 }

3) code explanation: the next page after mvcpager is rendered to the client is <a href = "manage/main/1"> </a>, therefore, when querying, you can only submit query conditions to the server in get mode. In view code, we need to modify the form submission method.

 
1@ Using (html. beginform ("Main","Manage", Formmethod. Get ))

After receiving the parameters, the parameters are displayed in the text box. viewdata ["username"] is used for data transmission between control and view.

4) The effect is as follows:

2. Modify

1) view code:

 1   @ Model mvc3.demomodel. User 2   @ Using (html. beginform ())  3   {  4   @ Html. labelfor (user => User. username)  5       <  BR  />  6   @ Html. textboxfor (user => User. username)  7       <  BR  /> 8   @ Html. labelfor (user => User. PHONE)  9       <  BR  />  10   @ Html. textboxfor (user => User. PHONE)  11       <  BR  />  12   @ Html. labelfor (user => User. Residential)  13  @ Html. dropdownlistfor (user => User. Residential, (selectlist) viewbag. viewresidential)  14   @ Html. labelfor (user => User. unitno)  15   @ Html. dropdownlistfor (user => User. unitno, (selectlist) viewbag. viewunitno)  16   @ Html. labelfor (user => User. floorno)  17   @ Html. dropdownlistfor (user => User. floorno, (selectlist) viewbag. viewfloorno)  18   @ Html. labelfor (user => User. doorplateno) 19   @ Html. dropdownlistfor (user => User. doorplateno, (selectlist) viewbag. viewdoorplateno)  20       <  BR  />  21   @ Html. hiddenfor (user => User. userid)  22       <  Input  Type  = "Submit"  Value  = "Modify"  />  23 }

2) control code:

 1           Public Actionresult useredit ( Int  ID)  2   {  3               //  Retrieve user information  4               If (ID! = 0  )  5  {  6 Model. User user = demorepository. User. Get ( New Model. User () {userid = Id });  7   8                   //  Retrieve the data and use helper to break down the data  9 Addresshelper = Addresshelper. getinstance ();  10   Addresshelper. getresidetialitem (getlist ());  11                  //  Reselect and upload viewbag to view  12 Viewbag. viewresidential = New Selectlist (addresshelper. residetialitem, "  Value  " , "  Text  "  , User. Residential );  13 Viewbag. viewfloorno = New Selectlist (addresshelper. floornoitem,"  Value  " , "  Text  "  , User. floorno );  14 Viewbag. viewunitno = New Selectlist (addresshelper. unitnoitem, "  Value  " , "  Text  " , User. unitno );  15 Viewbag. viewdoorplateno = New Selectlist (addresshelper. doorplatenoitem, "  Value  " , "  Text  "  , User. doorplateno );  16                   Return  View (User );  17   } 18               Return  View ();  19 }

3) code explanation:

 
1Viewbag. viewresidential =NewSelectlist (addresshelper. residetialitem,"Value","Text", User. Residential );

This is to reverse select the data in the database to the view.

4) running effect:

3. Delete

1) view code:

1@ Html. actionlink ("delete", "userremove", new {id = user. userid}, new {@ onclick = "Return confirm ('Are you sure you want to delete it? ');"})<Span>---</Span> 

2) control code

 1           //  Delete a user  2           Public   Void Userremove ( Int  ID, formcollection)  3  {  4 Model. User user = New Model. User () {userid = Id };  5   Demorepository. User. Remove (User );  6 MessageBox. showandredirect ( This , "  Deleted successfully!  " , "  /Manage/main/  " );  7 }

3) code explanation:

"Are you sure you want to delete it?" is added to the view ?" Dialog box.

In ciontrol: int ID, formcollection: delete In httppost mode.

4) running effect:

4. Download Code

[Click to download]

Copyright: http://www.cnblogs.com/iamlilinfeng

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.