Introduction to editing and deleting instances for Asp. Mvc 2.0 users (5) and asp. mvc instances

Source: Internet
Author: User
Tags actionlink

Introduction to editing and deleting instances for Asp. Mvc 2.0 users (5) and asp. mvc instances

This section describes how to modify and delete user information, including the following:
1. Show All Users
2. edit a user
3. delete a user
 

1. Show All Users
We can query all user information and display it on the page as a table, as shown below:

 

First, display all user information on the index page. Find the controller corresponding to the index page, find all user information, and put the searched user set in viewdata.
 Controller code:

Public ActionResult Index () {// query all users 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> email </td> <td> edit </td> <td> delete </td> </tr> <% foreach (var item in (ViewData ["users"] as IEnumerable <MvcLogin. models. userModels>) {%> <tr> <td> <%: item. userName %> </td> <%: item. userPwd %> </td> <%: item. email %> </td> <td> edit <%: Html. actionLink ("edit", "EditUser", "user", new {userName = item. userName}, null) %> </td> <%: Html. actionLink ("delete", "DelUser", "user", new {userName = item. userName}, null) %> </td> </tr> <% }%> </table>

Click the edit button next to each row of data to go To the Edit page. Next let's take a look at the editing page
2. edit a user
First, let's take a look at

Click the edit link of each row to go To the Edit page. The current user information is displayed.
First, let's take a look at the controller corresponding to the editing page:

/// <Summary> /// switch to the editing page /// </summary> /// <param name = "userName"> </param> /// <returns> </returns> public ActionResult EditUser (string userName) {// obtain user information according to the 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, the user information is displayed on the page. In this place, the page information is displayed using viewdata.
Page code

<Form id = "form1" method = "post" action = "/user/edituser? Username = <%: ViewData ["username"]. toString () %> "> <div> modify user information <table class =" style1 "> <tr> <td class =" style2 "> </td> <td class =" style3"> </td> </tr> <td class = "style2"> User Name: </td> <td class = "style3"> <input type = "text" id = "txtUserName" name = "txtUserName" disabled = "disabled" value = "<%: viewData ["username"]. toString () %> "/> </td> </tr> <td class =" style2 "> password: </td> <td cl Ass = "style3"> <input type = "text" id = "txtUserPwd" name = "txtUserPwd" value = "<%: ViewData [" userPwd "]. toString () %> "/> </td> </tr> <td class =" style2 "> Email: </td> <td class = "style3"> <input type = "text" id = "txtemail" name = "txtemail" value = "<%: viewData ["email"]. toString () %> "/> </td> </tr> <td class =" style2 "> </td> <td class =" style3 "> <input id =" Button1 "type =" submit "value = "Submit"/> </td> </tr> </table> <% if (ViewData ["errMsg"]! = Null) {%> <%: ViewData ["errMsg"]. ToString () %> <% }%> </div> </form>

 
Submit modification Information
After modifying the user information on the editing page, click Submit to submit the user information.
Let's take a 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"] = "userName and password cannot be blank "; return EditUser (userName);} else {// update database bool result = new Models. sqlHelper (). updateUser (userName, userPwd, emai L); if (result) {// turn to the 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 submitting the controller, we use Request. Form to obtain the user input content. After the submission is successful, go to the INDEX homepage.
 
3. delete a user.
Click the delete link to delete the corresponding controller based on the current user name.
 

/// <Summary> /// delete a user /// </summary> /// <param name = "userName"> </param> /// <returns> </returns> public ActionResult DelUser (string userName) {bool result = 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 whole process of editing and deleting instances for Asp. Mvc 2.0 users. I hope that learning Asp. Mvc 2.0 five sections will help you better master the basic functions of Asp. Mvc 2.0.

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.