ASP.MVC 2.0 User's edit and delete example explanation (5) _ Practical Skills

Source: Internet
Author: User

This section to show you how to modify the user information and delete users, mainly including the following content
1. Show All Users
2. Edit User
3. Delete User

1. Show All Users
we have all the user information query out, in tabular form on the page display, the effect chart is as follows:

First, all user information is displayed on the index page. Find the Controller for the index page, then find out all the user information, and put the collection of users in the ViewData.
Controller code:

Public ActionResult Index () 
    { 
      //query out all user 
      DataSet ds = new Models.sqlhelper (). GetAllUsers (); 
      if (Ds!=null&&ds. Tables[0]. rows.count>0) 
      { 
        list<models.usermodels> lists = new list<models.usermodels> (); 
 
        for (int i = 0; I < ds. Tables[0]. Rows.Count; i++) 
        { 
          Models.usermodels model = new Models.usermodels (); 
          Model. UserName = ds. Tables[0]. rows[i]["UserName"]. ToString (); 
          Model. Userpwd = ds. Tables[0]. rows[i]["Userpwd"]. ToString (); 
          Model. Email = ds. Tables[0]. rows[i]["Email"]. ToString (); 
          Lists. ADD (model); 
        } 
        if (lists. count>0) 
        { 
          viewdata["users"] = Lists 
        } 
       
      } return View (); 
    

Index page Code

 <table style= "BORDER-BOTTOM-WIDTH:1PX;" > <tr> <td> user name </td> <td> password </td> <td> mailbox </td> <t d> Edit </td> <td> Delete </td> </tr> <%foreach (var item in (viewdata["users") as Ienu merable<mvclogin.models.usermodels>)) {%> <tr> <td> <%:item. UserName%> </td> <td><%:item. Userpwd%></td> <td><%:item. Email%></td> <td> Edit <%:html.actionlink ("edit", "Edituser", "user", new {Username=item. Username},null)%></td> <td><%:html.actionlink ("delete", "Deluser", "user", new {Username=item. Username},null)%></td> </tr> <%}%> </table> 

Click on the Edit button behind each row of data to move to the edit page. Next, let's look at the edit page
2. Edit User
First, let's look at the effect of the edit page.

Click on the edit link for each line to edit the page to display the current user information.
First, let's look at the corresponding controller of the edit page:

<summary> 
    ///Edit page 
    ///</summary> 
    ///<param name= "UserName" ></param> 
    ///<returns></returns> public 
    actionresult edituser (string userName) 
    { 
      //Get user information based on user name 
      DataSet ds = new Models.sqlhelper (). Getsingleuser (userName); 
      if (ds!= null && ds. Tables[0]. Rows.Count > 0) 
      { 
        viewdata["username"] = ds. Tables[0]. rows[0]["username"]. ToString (); 
        viewdata["userpwd"] = ds. Tables[0]. rows[0]["Userpwd"]. ToString (); 
        viewdata["Email" = ds. Tables[0]. rows[0]["Email"]. ToString (); 
        Return View ("Edituser"); 
      } 
      else 
      {return 
        View (' error '); 
      } 
    } 

   then displays the user information on the page, where we display the page information in ViewData.
  page Code

<form id= "Form1" method= "Post" action= "/user/edituser?username=<%:viewdata[" "username"]. ToString ()%> "> <div> modifying User information <table class=" Style1 "> <tr> <td class=" 
          Style2 "> </td> <td class=" Style3 "> </td> <td> 
        </td> </tr> <tr> <td class= "style2" > User name:</td> &LT;TD class= "Style3" > <input type= "text" id= "txtUserName" name= "txtUserName" "disabled=" Disabled "value=" & lt;%:viewdata["username"]. 
      ToString ()%> "/> </td> <td> </td> </tr> 
          <tr> <td class= "style2" > Password:</td> <td class= "Style3" > <input type= "text" id= "txtuserpwd" name= "Txtuserpwd" value= "<%:viewdata[" Userpwd "]. 
        ToString ()%> "/> </td><td> </td> </tr> <tr> <td class= "Style2" > Mailbox: &L t;/td> <td class= "Style3" > <input type= "text" id= "Txtemail" name= "Txtemail" value= "<%:v" iewdata["Email"]. ToString ()%> "/> </td> <td> </td> </tr> <tr > <td class= "style2" > </td> <td class= "Style3" > <input id= "Button1" type= "Submit" value= "submitted"/></td> <td> </td> </tr> ;/table> <%if (viewdata["errmsg"]!= null) {%> <%:viewdata["ErrMsg"]. 
 ToString ()%> <%}%> </div> </form>

&NBSP
submit Modify
After modifying the user information on the edit page, click the Submit button to submit the user information.
Let's look at submitting the corresponding controller

[HttpPost] public ActionResult Edituser () {String userName = request.querystring["UserName"]. 
      ToString (); String userpwd = request.form["Txtuserpwd"]. 
      ToString (); string email = request.form["Txtemail"]. 
 
      ToString (); if (UserName = = "" | | 
        Userpwd = = "") {viewdata["errmsg"] = "User name and password cannot be empty"; 
      Return Edituser (UserName); else {//update database bool Result=new models.sqlhelper (). 
 
       UpdateUser (UserName, userpwd, email); if (result) {//Turn to homepage DataSet ds = new Models.sqlhelper (). 
         GetAllUsers (); if (ds!= null && ds. Tables[0]. 
 
           Rows.Count > 0) {list<models.usermodels> lists = new list<models.usermodels> (); for (int i = 0; I < ds. Tables[0]. Rows.Count; 
             i++) {Models.usermodels model = new Models.usermodels (); Model. UserName = ds. Tables[0]. rows[i]["UserName"]. ToSTring (); Model. Userpwd = ds. Tables[0]. rows[i]["Userpwd"]. 
             ToString (); Model. Email = ds. Tables[0]. rows[i]["Email"]. 
             ToString (); Lists. 
           ADD (model); } if (lists. 
           Count > 0) {viewdata["users"] = lists; 
       } return View ("index"); 
         else {viewdata["errmsg"] = "update Failed"; 
        
       Return Edituser (UserName); 
 } 
        
 
       
      }

In the submit controller, we use Request.Form to get what the user has entered. After the successful submission, turn to index home page.
 
3. Delete user.
Click the Delete link to remove the corresponding controller
&NBSP

According to the current username.

<summary>///Delete User///</summary>///<param name= "UserName" ></param>/// <returns></returns> public ActionResult Deluser (string userName) {bool = new Models. SqlHelper (). 
 
      Deluser (UserName); DataSet ds = new Models.sqlhelper (). 
      GetAllUsers (); if (ds!= null && ds. Tables[0]. 
 
        Rows.Count > 0) {list<models.usermodels> lists = new list<models.usermodels> (); for (int i = 0; I < ds. Tables[0]. Rows.Count; 
          i++) {Models.usermodels model = new Models.usermodels (); Model. UserName = ds. Tables[0]. rows[i]["UserName"]. 
          ToString (); Model. Userpwd = ds. Tables[0]. rows[i]["Userpwd"]. 
          ToString (); Model. Email = ds. Tables[0]. rows[i]["Email"]. 
          ToString (); Lists. 
        ADD (model); } if (lists. 
        Count > 0) {viewdata["users"] = lists; } return VIew ("index");  

The above is the ASP.MVC 2.0 users of the editing and deletion of the implementation of the whole process, I hope that through ASP.MVC 2.0 five content of learning can better help you master ASP.MVC 2.0 basic functions.

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.