一、数据展示
1、View代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="MvcApplication1.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Main</title> </head> <body> <table style="width: 100%; text-align: center;" cellspacing="0" cellpadding="0" border="1"> <tr style="background-color:navy;color:white;"> <td>序号</td> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>班级</td> </tr> <% List<Users> ulist = new UsersData().Select(); foreach (Users u in ulist) { %> <tr> <td><%=u.ids %></td> <td><%=u.username %></td> <td><%=u.password %></td> <td><%=u.nickname %></td> <td><%=u.sex %></td> <td><%=u.birthday %></td> <td><%=u.nation %></td> <td><%[email protected] %></td> </tr> <%} %> </table> </body> </html>
View视图展示页面
2、页面展示
3、属性扩展 性别、生日、民族、班级
代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="MvcApplication1.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Main</title> </head> <body> <table style="width: 100%; text-align: center;" cellspacing="0" cellpadding="0" border="1"> <tr style="background-color:navy;color:white;"> <td>序号</td> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>班级</td> </tr> <% List<Users> ulist = new UsersData().Select(); foreach (Users u in ulist) { %> <tr> <td><%=u.ids %></td> <td><%=u.username %></td> <td><%=u.password %></td> <td><%=u.nickname %></td> <td><%=Convert.ToBoolean(u.sex)?"男":"女" %></td> <td><%=Convert.ToDateTime(u.birthday).ToString("yyyy年MM月dd日") %></td> <td><%=u.Nation1.nationname %></td> <td><%=u.Class1.classname %></td> </tr> <%} %> </table> </body> </html>
View视图展示页面
页面展示:
二、添加数据
1、设置<添加>按钮
代码:
<a href="/Home/Insert">添加</a>
2、Home控制器下Insert动作代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Insert</title> </head> <body> <h1>添加新用户</h1> <span>用户名:</span><input type="text" /><br /> <span>密码:</span><input type="text" /><br /> <span>确认密码:</span><input type="text" /><br /> <span>昵称:</span><input type="text" /><br /> <span>性别:</span><input type="text" /><br /> <span>生日:</span><input type="text" /><br /> <span>民族:</span><input type="text" /><br /> <span>班级:</span><input type="text" /><br /> <input type="submit" value="添加" /> </body> </html>
Home控制器下Insert动作的代码
页面展示:
3、<添加>按钮代码 提交按钮所在的form表单
action:提交到的服务端
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Insert</title> </head> <body> <form action="/Hoome/Insert1" method="post"> <h1>添加新用户</h1> <span>用户名:</span><input type="text" name="username" /><br /> <span>密码:</span><input type="text" name="password" /><br /> <span>确认密码:</span><input type="text" name="password1" /><br /> <span>昵称:</span><input type="text" name="nickname" /><br /> <span>性别:</span><input type="text" name="sex" /><br /> <span>生日:</span><input type="text" name="birthday" /><br /> <span>民族:</span><input type="text" name="nation" /><br /> <span>班级:</span><input type="text" name="class" /><br /> <input type="submit" value="添加" /> </form> </body> </html>
form表单提交数据
控制器下动作代码:
//向数据库添加数据并跳转页面 public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation, string @class) { Users u = new Users(); u.username = username; u.password = password; u.nickname = nickname; u.sex = Convert.ToBoolean(sex); u.birthday = Convert.ToDateTime(birthday); u.nation = nation; [email protected] = @class; using (Data0928DataContext con = new Data0928DataContext()) { con.Users.InsertOnSubmit(u); con.SubmitChanges(); } return RedirectToAction("Main", "Home"); }
接收form提交的数据,向数据库添加数据,并重新定向
三、删除数据(地址栏传值)
1、添加“删除”操作列
<tr> <td><%=u.ids %></td> <td><%=u.username %></td> <td><%=u.password %></td> <td><%=u.nickname %></td> <td><%=Convert.ToBoolean(u.sex)?"男":"女" %></td> <td><%=Convert.ToDateTime(u.birthday).ToString("yyyy年MM月dd日") %></td> <td><%=u.Nation1.nationname %></td> <td><%=u.Class1.classname %></td> <td><a href="/Home/Delete?Duname=<%=u.username %>">删除</a></td> </tr>
添加“删除”操作列
2、控制器动作代码
//删除数据并重新定向 public ActionResult Delete() { string UName = Request["Duname"]; using (Data0928DataContext con = new Data0928DataContext()) { Users u = con.Users.Where(r => r.username == UName).FirstOrDefault(); if (u != null) { con.Users.DeleteOnSubmit(u); con.SubmitChanges(); } } return RedirectToAction("Main", "Home"); }
删除数据并重新定向
3、弹出提示框
<td><a href="/Home/Delete?Duname=<%=u.username %>" onclick="return confirm(‘确定要删除吗?‘)">删除</a></td>
四、修改数据(路由传值+ViewBag传值)
1、路由传值 - 从view界面传一个值到控制器下的动作中,值为string类型
<a href="/Home/Update/<%=u.username %>">修改</a>
2、ViewBag传值 - 从动作将数据传递到View层中进行绑定,值为任意类型
//查询到数据,用ViewBag带到界面进行绑定 public ActionResult Update(string id) { Users u=null; using (Data0928DataContext con = new Data0928DataContext()) { u = con.Users.Where(r => r.username == id).FirstOrDefault(); } ViewBag.UUser = u; return View(); }
ViewBag传值
3、View层绑定数据 记得引用命名空间
<body> <% Users u = ViewBag.UUser as Users; %> <form action="/Home/Update1" method="post"> <h1>添加新用户</h1> <span>用户名:</span><input type="text" name="username" readonly="readonly" value="<%=u.username %>" /><br /> <span>密码:</span><input type="text" name="password" value="<%=u.password %>" /><br /> <span>昵称:</span><input type="text" name="nickname" value="<%=u.nickname %>" /><br /> <span>性别:</span><input type="text" name="sex" value="<%=u.sex %>" /><br /> <span>生日:</span><input type="text" name="birthday" value="<%=u.birthday %>" /><br /> <span>民族:</span><input type="text" name="nation" value="<%=u.nation %>" /><br /> <span>班级:</span><input type="text" name="class" value="<%[email protected] %>" /><br /> <input type="submit" value="确认修改" /> </form> </body>
view层ViewBag绑定数据
4、<确认修改>按钮代码
//form表单提交数据,自动组对象,需要name属性值与字段名完全一致 public ActionResult Update1(Users u) { Users uu = u; using (Data0928DataContext con = new Data0928DataContext()) { Users uuu = con.Users.Where(r=>r.username==u.username).FirstOrDefault(); uuu.password = uu.password; uuu.nickname = uu.nickname; uuu.sex = uu.sex; uuu.birthday = uu.birthday; uuu.nation = uu.nation; [email protected] = [email protected]; con.SubmitChanges(); } return RedirectToAction("Main","Home"); }
form提交数据,自动组对象后重新定向
时间: 2024-11-05 18:36:36