public ActionResult DeleteConfirmed(string id) { 教师 教师 = db.教师.Find(id); db.教师.Remove(教师); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "工号,姓名,性别,出生日期,所在系部,职称,个人简介,职务,邮箱,办公电话,家庭电话,手机,角色")] 教师 教师) { if (ModelState.IsValid) { db.Entry(教师).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(教师)); }
public ActionResult Create([Bind(Include = "工号,姓名,性别,出生日期,所在系部,职称,个人简介,职务,邮箱,办公电话,家庭电话,手机,角色")] 教师 教师) { if (ModelState.IsValid) { db.教师.Add(教师); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(教师)); }
// GET: 教师/Delete/5 public ActionResult Delete(string id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } 教师 教师 = db.教师.Find(id); if (教师 == null) { return(HttpNotFound()); } return(View(教师)); }
public ActionResult Add(教师 model) { try { //0.给新实体对象一个唯一的id(教师编号xxxx) var showidmax = db.教师.Select(s => s.教师编号).Max(); int id = showidmax + 1; model.教师编号 = id; //1. 将实体对象 a.加入 EF 对象容器中 db.教师.Add(model); //2.提交到数据库 完成添加 db.SaveChanges(); //5.更新完成,则命令 浏览器 重定向 到 /Teacher/TeacherList 方法 return(RedirectToAction("TeacherList", "Teacher")); } catch (Exception e) { return(Content("添加失败~" + e.Message)); } }
/// <summary> /// 执行删除操作(根据id) /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Del(int id) { try { //1.创建删除的对象 教师 teacher = new 教师() { 教师编号 = id }; //2.将对象 添加到EF 管理容器 db.教师.Attach(teacher); //3.将对象包装类的 状态 标识 为删除状态 db.教师.Remove(teacher); //4.更新到数据库 db.SaveChanges(); //5.更新成功,则命令浏览器 重定向 到/Teacher/TeacherList 方法 return(RedirectToAction("TeacherList", "Teacher")); } catch (Exception e) { return(Content("删除失败~~" + e.Message)); } }
/// <summary> /// 执行修改 /// </summary> /// <returns></returns> public ActionResult Modify(教师 model) { try { //1. 将实体对象 a.加入 EF 对象容器中,并b.获取 伪包装类对象 DbEntityEntry <教师> entry = db.Entry <教师>(model); //2. 将包装类对象的状态设置为 unchanged entry.State = System.Data.Entity.EntityState.Unchanged; //3.设置 被改变的属性 entry.Property(a => a.教师姓名).IsModified = true; entry.Property(a => a.性别).IsModified = true; entry.Property(a => a.院系编号).IsModified = true; //4.提交到数据库 完成修改 db.SaveChanges(); //5.更新完成,则命令 浏览器 重定向 到 /Teacher/TeacherList 方法 return(RedirectToAction("TeacherList", "Teacher")); } catch (Exception e) { return(Content("修改失败~" + e.Message)); } }
public ActionResult Modify(int id) { //1.根据id 查询 数据库,返回的集合中,拿到 第一个 实体对象 教师 teacher = (from d in db.教师 where d.教师编号 == id select d).FirstOrDefault(); //2.1生成 老师 院系 下拉框 列表集合 <option value="1">文本</option> IQueryable <院系> query = (from c in db.院系 where c.是否有效 == "是" select c) as IQueryable <院系>; List <SelectListItem> listItems = query.Select(c => new SelectListItem { Value = c.院系编号.ToString(), Text = c.院系名 }).ToList(); ViewBag.DeptList = listItems; //2.2生成 老师 性别 下拉框 列表集合 <option value="1">文本</option> Dictionary <String, String> MaleType = new Dictionary <string, string>(); MaleType.Add("性别", "男"); Dictionary <String, String> FemaleType = new Dictionary <string, string>(); FemaleType.Add("性别", "女"); List <Dictionary <String, String> > SexList = new List <Dictionary <string, string> >(); SexList.Add(MaleType); SexList.Add(FemaleType); List <SelectListItem> SexItems = SexList.Select(c => new SelectListItem { Value = c["性别"], Text = c["性别"] }).ToList(); ViewBag.SexList = SexItems; //3. 将teacher 传递给视图显示 // ViewBag // ViewData //“加载” 视图,使用view的构造函数,将 数据传给 视图上的 名为Model 的属性 return(View(teacher)); }