Asp.Mvc 2.0用户的编辑与删除实例讲解(5)

所属分类: 网络编程 / ASP.NET 阅读数: 551
收藏 0 赞 0 分享

这一节来给大家演示下怎么对用户信息进行修改和删除用户,主要包括以下内容
1.显示所有用户
2.编辑用户
3.删除用户
 

1.显示所有用户
  我们把所有用户信息查询出来,以表格形式在页面上显示,效果图如下:

 

首先把所有用户信息显示在index页面上.找到index页面对应的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> 

点击每行数据后面的编辑按钮,转向编辑页面。接下来我们看看编辑页面
2.编辑用户
 首先我们看下编辑页面的效果图 

 点击每行的编辑链接,转向编辑页面,显示当前用户信息。
首先我们看下编辑页面对应的controller:

/// <summary> 
    /// 转向编辑页面 
    /// </summary> 
    /// <param name="userName"></param> 
    /// <returns></returns> 
    public ActionResult EditUser(string userName) 
    { 
      //根据用户名获取用户信息 
      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"); 
      } 
    } 

  然后在页面上显示用户信息,在这个地方我们显示页面信息用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> 

 
提交修改信息
在编辑页面修改完用户信息后,点击提交按钮,会提交用户信息。
我们看下提交对应的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 
      {  
        //更新数据库 
       bool result=new Models.SqlHelper().UpdateUser(userName, userPwd, email); 
 
       if (result) 
       { 
         //转向主页 
         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获取用户输入的内容。提交成功后,转向INDEX首页。
 
3.删除用户.
点击删除链接,会根据当前的用户名,转向删除对应的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用户的编辑与删除实例的实现全过程,希望通过Asp.Mvc 2.0五节内容的学习可以更好地帮助大家掌握Asp.Mvc 2.0基本功能。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多