zoukankan      html  css  js  c++  java
  • MVC的增删改查

    基本都要使用C控制器中的两个action来完成操作,一个用于从主界面跳转到新页面、同时将所需操作的数据传到新界面,另一个则对应新界面的按钮,用于完成操作、将数据传回主界面以及跳转回主界面。根据不同情况使用不同的传值方法。

    在M模型层中定义所需的LinQ操作

    1.查

     1 <table style="100%;background-color:#0ff;text-align:center">
     2       <tr>
     3           <td>账号</td>
     4           <td>密码</td>
     5           <td>昵称</td>
     6           <td>性别</td>
     7           <td>生日</td>
     8           <td>民族</td>
     9           <td>操作</td>
    10       </tr>
    11       <%
    12           List<User> list = new UserData().Select();
    13           foreach(User U in list){
    14            %>
    15        <tr style="background-color:#cefef8">
    16           <td><%=U.UserName %></td>
    17           <td><%=U.PassWord %></td>
    18           <td><%=U.NickName%></td>
    19           <td><%=U.Sexstr %></td>
    20           <td><%=U.birStr %></td>
    21           <td><%=U.Nation1.NationName %></td>
    22            <td> <a href="Home/Update/<%=U.UserName %>">修改</a>
    23                <a class="del" href="Home/Delete?haha=<%=U.UserName %>">删除</a></td>
    24       </tr>
    25       <% }%>
    26   </table>

    2.删

    在C层添加动作

    1         public void Delete(string uname)
    2         {
    3             User u = con.User.Where(r => r.UserName == uname).FirstOrDefault();
    4             if (u != null)
    5             {
    6                 con.User.DeleteOnSubmit(u);
    7                 con.SubmitChanges();
    8             }
    9         }

    3.添加

    View中提交元素,表单元素使用form表单提交,按钮的使用submit,点击submit的时候会提交所在form表单中的数据,在控制器C中获取元素,在模型层M的写法,在C中调用。

      <form name="form1" action="Insert1" method="post">
    
                <%string a = ""; %>
                <h1>添加用户</h1>
                用户名:<input type="text" name="username" /><br />
                密码:<input type="text" name="password" /><br />
                昵称:<input type="text" name="nickname" /><br />
                性别:<input type="text" name="sex" /><br />
                生日:<input type="text" name="birthday" /><br />
                民族:<input type="text" name="nation" /><br />
                <input type="submit" value="添加" />
            </form>
    View Code
     1  public ActionResult Insert()
     2         {
     3             return View();
     4         }
     5 
     6         public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation)
     7         {
     8             Users u = new Users();
     9             u.UserName = username;
    10             u.PassWord = password;
    11             u.NickName = nickname;
    12             u.Sex = Convert.ToBoolean(sex);
    13             u.Birthday = Convert.ToDateTime(birthday);
    14             u.Nation = nation;
    15 
    16             new UsersData().Insert(u);
    17 
    18             return RedirectToAction("Index", "Home");
    19         }//接收form表单提交的数据

    4.修改

    同添加,需要两个action支持,一个主页面打开修改页面,一个修改按钮确定修改返回主页面

    从控制器传值到View使用ViewBag.包名=数据源。

    View中<%Users u=ViewBag.包名 as User; %>

    系统自生成的Users u 有可能缺少部分内容

    form表单中的action路径 action="/home/update"

     1   public ActionResult Update(string id)
     2         {
     3             User u = new UserData().Select(id);
     4 
     5             ViewBag.heihei1 = u;
     6             return View();
     7         }
     8         public ActionResult Update1(User u)
     9         {
    10             new UserData().Update(u);
    11 
    12             return RedirectToAction("Index");
    13         }
  • 相关阅读:
    团队作业6——展示博客(alpha阶段)
    第八天敏捷冲刺
    第七天敏捷冲刺
    第六天敏捷冲刺
    第五天敏捷冲刺
    第四天敏捷冲刺
    第三天敏捷冲刺
    linux查询内存使用情况
    input hidden 属性
    于Java网络爬虫---模拟txt文件上传操作。
  • 原文地址:https://www.cnblogs.com/zblc2016/p/6132681.html
Copyright © 2011-2022 走看看