Asp.Mvc 2.0用戶的編輯與刪除實(shí)例講解(5)
這一節(jié)來給大家演示下怎么對用戶信息進(jìn)行修改和刪除用戶,主要包括以下內(nèi)容
1.顯示所有用戶
2.編輯用戶
3.刪除用戶
1.顯示所有用戶
我們把所有用戶信息查詢出來,以表格形式在頁面上顯示,效果圖如下:
首先把所有用戶信息顯示在index頁面上.找到index頁面對應(yīng)的controller,然后查找出所有用戶信息,把查找出的用戶集合放在viewdata里面
Controller代碼:
public ActionResult Index() { //查詢出所有用戶 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頁面代碼
<table style="border-bottom-width:1px;"> <tr> <td>用戶名</td> <td>密碼</td> <td>郵箱</td> <td>編輯</td> <td>刪除</td> </tr> <%foreach (var item in (ViewData["users"] as IEnumerable<MvcLogin.Models.UserModels>) ) {%> <tr> <td> <%:item.UserName %> </td> <td><%:item.UserPwd %></td> <td><%:item.Email %></td> <td>編輯 <%:Html.ActionLink("編輯", "EditUser","user",new { userName=item.UserName},null)%></td> <td><%:Html.ActionLink("刪除", "DelUser", "user", new { userName=item.UserName},null)%></td> </tr> <% } %> </table>
點(diǎn)擊每行數(shù)據(jù)后面的編輯按鈕,轉(zhuǎn)向編輯頁面。接下來我們看看編輯頁面
2.編輯用戶
首先我們看下編輯頁面的效果圖
點(diǎn)擊每行的編輯鏈接,轉(zhuǎn)向編輯頁面,顯示當(dāng)前用戶信息。
首先我們看下編輯頁面對應(yīng)的controller:
/// <summary> /// 轉(zhuǎn)向編輯頁面 /// </summary> /// <param name="userName"></param> /// <returns></returns> public ActionResult EditUser(string userName) { //根據(jù)用戶名獲取用戶信息 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"); } }
然后在頁面上顯示用戶信息,在這個(gè)地方我們顯示頁面信息用viewdata來顯示。
頁面代碼
<form id="form1" method="post" action="/user/edituser?username=<%:ViewData["username"].ToString() %>"> <div> 修改用戶信息 <table class="style1"> <tr> <td class="style2"> </td> <td class="style3"> </td> <td> </td> </tr> <tr> <td class="style2"> 用戶名:</td> <td class="style3"> <input type="text" id="txtUserName" name="txtUserName" disabled="disabled" value="<%:ViewData["username"].ToString() %>" /> </td> <td> </td> </tr> <tr> <td class="style2"> 密碼:</td> <td class="style3"> <input type="text" id="txtUserPwd" name="txtUserPwd" value="<%:ViewData["userPwd"].ToString() %>"/> </td> <td> </td> </tr> <tr> <td class="style2"> 郵箱:</td> <td class="style3"> <input type="text" id="txtemail" name="txtemail" value="<%:ViewData["email"].ToString() %>" /> </td> <td> </td> </tr> <tr> <td class="style2"> </td> <td class="style3"> <input id="Button1" type="submit" value="提交" /></td> <td> </td> </tr> </table> <%if (ViewData["errMsg"] != null) {%> <%:ViewData["errMsg"].ToString()%> <%} %> </div> </form>
提交修改信息
在編輯頁面修改完用戶信息后,點(diǎn)擊提交按鈕,會(huì)提交用戶信息。
我們看下提交對應(yīng)的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"] = "用戶名和密碼不能為空"; return EditUser(userName); } else { //更新數(shù)據(jù)庫 bool result=new Models.SqlHelper().UpdateUser(userName, userPwd, email); if (result) { //轉(zhuǎn)向主頁 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"] = "更新失敗"; return EditUser(userName); } }
在提交controller中,我們使用Request.Form獲取用戶輸入的內(nèi)容。提交成功后,轉(zhuǎn)向INDEX首頁。
3.刪除用戶.
點(diǎn)擊刪除鏈接,會(huì)根據(jù)當(dāng)前的用戶名,轉(zhuǎn)向刪除對應(yīng)的controller
/// <summary> /// 刪除用戶 /// </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");
以上就是Asp.Mvc 2.0用戶的編輯與刪除實(shí)例的實(shí)現(xiàn)全過程,希望通過Asp.Mvc 2.0五節(jié)內(nèi)容的學(xué)習(xí)可以更好地幫助大家掌握Asp.Mvc 2.0基本功能。
- asp.net之生成驗(yàn)證碼的方法集錦(一)
- 詳解ASP.NET七大身份驗(yàn)證方式以及解決方案
- ASP.NET中驗(yàn)證控件的使用方法
- ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布(1)
- ASP.NET MVC3模板頁的使用(2)
- ASP.NET MVC4之js css文件合并功能(3)
- Asp.Mvc?2.0實(shí)現(xiàn)用戶注冊實(shí)例講解(1)
- Asp.Mvc 2.0實(shí)現(xiàn)用戶登錄與注銷功能實(shí)例講解(2)
- Asp.Mvc?2.0用戶客戶端驗(yàn)證實(shí)例講解(3)
- 創(chuàng)建第一個(gè)ASP.NET應(yīng)用程序(第1節(jié))
- ASP.NET網(wǎng)站模板的實(shí)現(xiàn)(第2節(jié))
- ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn)(第3節(jié))
- ASP.NET實(shí)現(xiàn)用戶注冊和驗(yàn)證功能(第4節(jié))
- ASP.NET在線文本編輯控件的使用(第6節(jié))
- ASP.NET實(shí)現(xiàn)數(shù)據(jù)的添加(第10節(jié))
- ASP.NET用戶注冊實(shí)戰(zhàn)(第11節(jié))
- Asp.Mvc 2.0用戶服務(wù)器驗(yàn)證實(shí)例講解(4)
- ASP.NET對大文件上傳的解決方案
- Asp.Net上傳圖片同時(shí)生成高清晰縮略圖
- ASP.NET MVC5添加驗(yàn)證(4)
相關(guān)文章
asp.net 在線編輯word文檔 可保存到服務(wù)器
使用說明:該方法只在office xp 和 2003上 測試通過,2000及以下 版本沒試。2010-01-01ASP.NET性能優(yōu)化小結(jié)(ASP.NET&C#)
ASP.NET性能優(yōu)化,提高頁面的執(zhí)行效率與下載速度,等很多需要考慮的細(xì)節(jié),編程人員值得參考下。2011-01-01利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法
這篇文章介紹了利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法,有需要的朋友可以參考一下,希望對你有所幫助2013-07-07關(guān)于DDD:管理"工作單元實(shí)例"的兩種模式的使用方法
本篇文章介紹了,關(guān)于DDD:管理"工作單元實(shí)例"的兩種模式的使用方法。需要的朋友參考下2013-04-04ASP.NET中防止頁面刷新造成表單重復(fù)提交執(zhí)行兩次操作
本文主要介紹在Session存儲(chǔ)唯一標(biāo)識(shí)Token,通過和后臺(tái)對比,以實(shí)現(xiàn)防止刷新提交表單的問題。2016-04-04asp.net richTextBox中高亮顯示選中字符串或文本
最近開發(fā)程序需要對一段文本中的某個(gè)字符串進(jìn)行高亮顯示,網(wǎng)上找了下資料2011-11-11.NET6創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)步驟
本文主要介紹了.NET6創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06