时间:2021-07-01 10:21:17 帮助过:1人阅读
controller:控制器
action:将这个也页面转到某个页面去。
using mvc_1_.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using mvc_1_.Models; namespace mvc_1_.Controllers { public class indexController : Controller { // // GET: /index/ //public ActionResult Show() //{ // return View(); //} public ActionResult Index()//查询 { //动作所对应的视图模版 ViewBag.date = new da().select(); return View(); } public ActionResult Add()//添加 { return View(); } public ActionResult Insert(string code,string name,string sex,DateTime shengri,string banji)//添加弹出视图 { //送到数据库中去 new da().Insert(code,name,sex,shengri,banji ); //跳转到index的页面上 return RedirectToAction("Index"); } public ActionResult A(string id)//修改主函数 (弹出) { student data = new da().Select(id); //ViewBag把数据带到视图中去 ViewBag.Data = data; return View(); } public ActionResult Update(string code, string name, string sex, DateTime shengri, string banji)//修改 { new da().Update(code,name,sex,shengri,banji); return RedirectToAction("Index"); } public ActionResult Delete(string id)//删除 { try { new da().Delete(id); return RedirectToAction("Index"); } catch { return RedirectToAction("Index", "Error"); } } } }
(二) 数据库函数
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; namespace mvc_1_.Models { public class da { private DataDataContext Context = new DataDataContext();//数据库 public List<student> select()//表的名字 { return Context.student.ToList();//把他输出来 } public void Insert(string code, string name, string sex, DateTime shengri, string banji)//添加 { student data = new student { Sno=int.Parse(code), Sname=name, Ssex=sex, Sbirthday=shengri, Class=banji }; Context.student.InsertOnSubmit(data); Context.SubmitChanges(); } public void Update(string code, string name, string sex, DateTime shengri, string banji)//修改i { var query = Context.student.Where(p => p.Sname == name); if (query.Count() > 0) { student data = query.First(); //data.Sno = int.Parse(code); data.Sname = name; data.Ssex = sex; data.Sbirthday = shengri; data.Class = banji; } Context.SubmitChanges(); } public student Select(string name)//修改查询 { var query = Context.student.Where(p => p.Sname== name); if (query.Count() > 0) return query.First(); return null; } public void Delete(string name)//删除 { var query = Context.student.Where(p => p.Sname == name); if (query.Count() > 0) { student data = query.First(); Context.student.DeleteOnSubmit(data); Context.SubmitChanges(); } } } }
MVC 数据库增删改查(1)
标签: