using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Mvc1.Models; namespace Mvc1.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } //添加 public ActionResult Insert() { return View(); } public ActionResult Insert1(string username,string password,string nickname,bool gender,DateTime birthday,string nation,string area) { Users u = new Users(); u.username = username; u.password = password; u.nickname = nickname; u.gender = gender; u.birthday = birthday; u.nation = nation; u.area = area; new UsersData().Insert(u); return RedirectToAction("Index", "Home"); } //删除 public ActionResult Delete(string id) { new UsersData().Delete(id); return RedirectToAction("Index", "Home"); } //修改 public ActionResult Update(string id) { Users u = new UsersData().Select1(id); ViewBag.data = u; return View(); } public ActionResult Update1(Users u) { new UsersData().Update(u); return RedirectToAction("Index","Home"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Mvc1.Models { public class UsersData { DataClasses1DataContext conn = new DataClasses1DataContext(); /// <summary> /// 查询全部数据 /// </summary> /// <returns>List</returns> public List<Users> Select() { return conn.Users.ToList(); } /// <summary> /// 添加 /// </summary> /// <param name="u">Users u</param> public void Insert(Users u) { conn.Users.InsertOnSubmit(u); conn.SubmitChanges(); } /// <summary> /// 删除 /// </summary> /// <param name="username">username</param> public void Delete(string username) { Users u = conn.Users.Where(r => r.username == username).FirstOrDefault(); conn.Users.DeleteOnSubmit(u); conn.SubmitChanges(); } /// <summary> /// 显示指定数据 /// </summary> /// <param name="username">username</param> /// <returns>Users</returns> public Users Select1(string username) { return conn.Users.Where(r => r.username == username).FirstOrDefault(); } /// <summary> /// 修改 /// </summary> /// <param name="u"></param> public void Update(Users u) { Users uu = conn.Users.Where(r => r.username == u.username).FirstOrDefault(); uu.password = u.password; uu.nickname = u.nickname; uu.birthday = u.birthday; uu.gender = u.gender; uu.nation = u.nation; uu.area = u.area; conn.SubmitChanges(); } } }
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="Mvc1.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <table style="background-color:navy;100%;text-align:center;"> <tr style="color:white;"> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>地区</td> <td>操作</td> </tr> <% List<Users> list = new UsersData().Select(); foreach(Users u in list) { %> <tr style="background-color:white;"> <td><%=u.username %></td> <td><%=u.password %></td> <td><%=u.nickname %></td> <td><%=u.gender?"男":"女" %></td> <td><%=u.birthday.ToString("yyyy-MM-dd") %></td> <td><%=u.Nation1.Name %></td> <td><%=u.area %></td> <td> <a href="Home/Update/<%=u.username %>">修改</a> <a href="Home/Delete/<%=u.username %>">删除</a> </td> </tr> <% } %> </table> <a href="Home/Insert">添加</a> </body> </html>
<%@ 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="Insert1" method="post"> <div> <h1>添加页面</h1> 用户名:<input type="text" name="username"/><br /><br /> 密 码:<input type="text" name="password"/><br /><br /> 昵 称:<input type="text" name="nickname"/><br /><br /> 性 别:<input type="text" name="gender"/><br /><br /> 生 日:<input type="text" name="birthday"/><br /><br /> 民 族:<input type="text" name="nation"/><br /><br /> 地 区:<input type="text" name="area"/><br /><br /> <input type="submit" value="添加"/> </div> </form> </body> </html>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="Mvc1.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Update</title> </head> <body> <form action="/Home/Update1" method="post"> <% Users u = ViewBag.data as Users; if (u != null) { %> <div> <h1>修改页面</h1> 用户名:<input type="text" name="username" value="<%=u.username %>"/><br /> <br /> 密 码:<input type="text" name="password" value="<%=u.password %>"/><br /> <br /> 昵 称:<input type="text" name="nickname" value="<%=u.nickname %>"/><br /> <br /> 性 别:<input type="text" name="gender" value="<%=u.gender %>"/><br /> <br /> 生 日:<input type="text" name="birthday" value="<%=u.birthday %>"/><br /> <br /> 民 族:<input type="text" name="nation" value="<%=u.nation %>"/><br /> <br /> 地 区:<input type="text" name="area" value="<%=u.area %>"/><br /> <br /> <input type="submit" value="修改" /> </div> <% } else { %> <h1>未查询到数据!</h1> <% } %> </form> </body> </html>