這篇文章主要講解了“如何實(shí)現(xiàn)Asp.Mvc 2.0用戶的編輯與刪除”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何實(shí)現(xiàn)Asp.Mvc 2.0用戶的編輯與刪除”吧!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的定陶網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!1.顯示所有用戶
我們把所有用戶信息查詢出來(lái),以表格形式在頁(yè)面上顯示,效果圖如下:
首先把所有用戶信息顯示在index頁(yè)面上.找到index頁(yè)面對(duì)應(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頁(yè)面代碼
<table > <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)向編輯頁(yè)面。接下來(lái)我們看看編輯頁(yè)面
2.編輯用戶
首先我們看下編輯頁(yè)面的效果圖
點(diǎn)擊每行的編輯鏈接,轉(zhuǎn)向編輯頁(yè)面,顯示當(dāng)前用戶信息。
首先我們看下編輯頁(yè)面對(duì)應(yīng)的controller:
/// <summary> /// 轉(zhuǎn)向編輯頁(yè)面 /// </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"); } }
然后在頁(yè)面上顯示用戶信息,在這個(gè)地方我們顯示頁(yè)面信息用viewdata來(lái)顯示。
頁(yè)面代碼
<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>
提交修改信息
在編輯頁(yè)面修改完用戶信息后,點(diǎn)擊提交按鈕,會(huì)提交用戶信息。
我們看下提交對(duì)應(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ù)庫(kù) bool result=new Models.SqlHelper().UpdateUser(userName, userPwd, email); if (result) { //轉(zhuǎn)向主頁(yè) 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首頁(yè)。
3.刪除用戶.
點(diǎn)擊刪除鏈接,會(huì)根據(jù)當(dāng)前的用戶名,轉(zhuǎn)向刪除對(duì)應(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");
感謝各位的閱讀,以上就是“如何實(shí)現(xiàn)Asp.Mvc 2.0用戶的編輯與刪除”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)如何實(shí)現(xiàn)Asp.Mvc 2.0用戶的編輯與刪除這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
分享標(biāo)題:如何實(shí)現(xiàn)Asp.Mvc2.0用戶的編輯與刪除-創(chuàng)新互聯(lián)
文章分享:http://www.rwnh.cn/article14/cspgde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App設(shè)計(jì)、做網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容