// // GET: /Home/ TestTryEntities Db = new TestTryEntities(); public ActionResult Index() { return View(); } /// <summary> /// 查询 /// </summary> /// <returns></returns> public ActionResult GetStudent() { int pageIndex = 1;//页码 int pageSize = 10;//每页条数 //var data = Db.Student.OrderBy(r => r.ID) // .Skip(pageSize * (pageIndex - 1)).Take(pageSize) // .Select(r => new { r.ID, r.Name, r.Password }); var data1 = (from r in Db.Student join t in Db.Class on r.ClassID equals t.ID orderby r.ID select new { r.ID, r.Name, r.Password, t.ClassName }) .Skip(pageSize * (pageIndex - 1)).Take(pageSize); int total = Db.Student.Count();//总条数 //构造成Json的格式传递 var result = new { total = total, rows = data1 }; return Json(result, JsonRequestBehavior.AllowGet); } //添加 public ActionResult Add() { var data = Db.Class.Select(r => new SelectListItem() { Text = r.ClassName, Value = SqlFunctions.StringConvert((double)r.ID) }).ToList(); ViewData["Class"] = data; return View(); } [HttpPost] public ActionResult Add(Model.Student model) { Db.Student.Add(model); Db.SaveChanges(); return View("Index"); } /// <summary> /// 修改 /// </summary> /// <param name="ID"></param> /// <returns></returns> public ActionResult Edit(int ID) { var model = Db.Student.Where(r => r.ID == ID).Select(r => r).FirstOrDefault(); ViewData["modelStudent"] = model; var data = Db.Class.Select(r => new SelectListItem() { Text = r.ClassName, Value = SqlFunctions.StringConvert((double)r.ID), Selected = (r.ID == model.ClassID) }).ToList(); ViewData["Class"] = data; return View(model); } [HttpPost] public ActionResult Edit(Model.Student model) { Db.Student.Attach(model); Db.Entry<Student>(model).State = System.Data.EntityState.Modified; if (Db.SaveChanges() > 0) { return Content("OK"); } else { return Content("修改失败"); } } /// <summary> /// 删除 /// </summary> /// <param name="ID"></param> /// <returns></returns> public ActionResult Delete(int ID) { //var model = Db.Student.Where(r => r.ID == ID).Select(r => r).FirstOrDefault(); //if (model == null) //{ // return Script("alert(‘验证失败!)"); // return View("Index"); //} //Db.Student.Remove(model); //Db.SaveChanges(); var model1 = new Model.Student() { ID = ID }; if (model1 == null) { return View("Index"); } Db.Student.Attach(model1); Db.Entry<Student>(model1).State = System.Data.EntityState.Deleted; Db.SaveChanges(); return View("Index"); } public ActionResult AddMulti() { return View(); } [HttpPost] public ActionResult AddMulti(Mvctry.Models.ClassStudent model) { return View(); } }
时间: 2024-10-25 03:30:09