时间:2021-07-01 10:21:17 帮助过:3人阅读
增加视图代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Add</title> </head> <body> <div> <h1>添加人员</h1> <div> <form id="f1" name="f1" action="/Home/Insert" method="post"> 代号:<input type="text" name="code" /><br/> 姓名:<input type="text" name="name" /><br/> 性别:<input type="text" name="sex" /><br/> 民族:<input type="text" name="nation" /><br/> 生日:<input type="text" name="birthday" /><br/> <input type="submit" value="添加" /> </form> </div> </div> </body> </html>
修改视图代码:
<%@ 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>Edit</title> </head> <body> <div> <h1>修改人员</h1> <div> <form id="f1" name="f1" action="/Home/Update" method="post"> <% Info data = ViewBag.Data as Info; if(data != null) { %> 代号:<input readonly="readonly" type="text" name="code" value="<%=data.Code %>" /><br/> 姓名:<input type="text" name="name" value="<%=data.Name %>" /><br/> 性别:<input type="text" name="sex" value="<%=data.Sex %>"/><br/> 民族:<input type="text" name="nation" value="<%=data.Nation %>" /><br/> 生日:<input type="text" name="birthday" value="<%=data.Birthday %>" /><br/> <input type="submit" value="更新" /> <%} %> </form> </div> </div> </body> </html>
主页目录视图代码:
<%@ 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>Index</title> </head> <body> <div> <ul> <% List<Info> list = ViewBag.Data as List<Info>; foreach(Info data in list) { %> <li> <a href="Home/Details/<%=data.Code %>"> <%=data.Name %></a> <a href="Home/Edit/<%=data.Code %>">修改</a> <a href="Home/Delete/<%=data.Code %>">删除</a> </li> <% } %> </ul> <a href="Home/Add">添加</a> </div> </body> </html>
Model层增删改查Linq方法代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication1.Models { public class InfoBF { private MyDBDataContext _Context = new MyDBDataContext(); public List<Info> Select() { return _Context.Info.ToList(); } public Info Select(string code) { var query = _Context.Info.Where(p=>p.Code == code); if (query.Count() > 0) { return query.First(); } return null; } public void Update(string code, string name, bool sex, string nation, DateTime birthday) { var query = _Context.Info.Where(p=>p.Code == code); if (query.Count() > 0) { Info data = query.First(); data.Name = name; data.Sex = sex; data.Nation = nation; data.Birthday = birthday; } _Context.SubmitChanges(); } public void Insert(string code, string name, bool sex, string nation, DateTime birthday) { Info data = new Info { Code = code, Name = name, Sex = sex, Nation = nation, Birthday = birthday }; _Context.Info.InsertOnSubmit(data); _Context.SubmitChanges(); } public void Delete(string code) { var query = _Context.Info.Where(p => p.Code == code); if (query.Count() > 0) { Info data = query.First(); _Context.Info.DeleteOnSubmit(data); _Context.SubmitChanges(); } } } }
MVC操作SQL数据库增删改查
标签: