当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Mvc 2.0用户的编辑与删除实例讲解(5)

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

跑狗图,150是移动的吗,佛山网址导航

这一节来给大家演示下怎么对用户信息进行修改和删除用户,主要包括以下内容
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基本功能。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网