资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何实现Asp.Mvc2.0用户的编辑与删除-创新互联

这篇文章主要讲解了“如何实现Asp.Mvc 2.0用户的编辑与删除”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现Asp.Mvc 2.0用户的编辑与删除”吧!

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的定陶网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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

 如何实现Asp.Mvc 2.0用户的编辑与删除

首先把所有用户信息显示在index页面上.找到index页面对应的controller,然后查找出所有用户信息,把查找出的用户集合放在viewdata里面
 Controller代码:


public ActionResult Index() 
    { 
      //查询出所有用户 
      DataSet ds = new Models.SqlHelper().GetAllUsers(); 
      if (ds!=null&&ds.Tables[0].Rows.Count>0) 
      { 
        List lists = new List(); 
 
        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页面代码


 
    
    用户名 
     密码 
     邮箱 
      编辑 
      删除 
    
   <%foreach (var item in (ViewData["users"] as IEnumerable) ) 
    {%> 
       
         
         <%:item.UserName %> 
         
        <%:item.UserPwd %> 
         
        <%:item.Email %> 
 
        编辑 <%:Html.ActionLink("编辑", "EditUser","user",new { userName=item.UserName},null)%> 
        <%:Html.ActionLink("删除", "DelUser", "user", new { userName=item.UserName},null)%> 
       
   <% } %> 
 
 

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

如何实现Asp.Mvc 2.0用户的编辑与删除

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



///  
    /// 转向编辑页面 
    ///  
    ///  
    ///  
    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来显示。
 页面代码


"> 
  
    修改用户信息                                                                                                                      用户名:                     " />                                                                                    密码:                      "/>                                                                        邮箱:                      " />                                                                                                                                            <%if (ViewData["errMsg"] != null)     {%>      <%:ViewData["errMsg"].ToString()%>    <%} %>    
      

提交修改信息
在编辑页面修改完用户信息后,点击提交按钮,会提交用户信息。
我们看下提交对应的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 lists = new List(); 
 
           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


///  
    /// 删除用户 
    ///  
    ///  
    ///  
    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 lists = new List(); 
 
        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.Mvc2.0用户的编辑与删除-创新互联
当前网址:http://cdkjz.cn/article/cspgde.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220