public ActionResult Index() { if (db.book.Count() == 0) //调用 (出错是没有设置主键 [Key]导入命名空间) { Book book = new Book() //创建book对象 { Author = "张三", BookName = "张三的书", Price = 1.5M }; db.book.Add(book); //添加 db.SaveChanges(); //保存 } List<Book> listbk = db.book.ToList(); //对象集合 return View(listbk); //传递集合 }
[AllowAnonymous] //解除限制 public ActionResult Login() { string type = "Yes"; if (type=="Yes") { //要设置一些信息,告诉程序已经登录了 //读取Cookies中的 string Name = User.Identity.Name; //获取cook已经存在的名字 FormsAuthentication.SetAuthCookie(Name, false); } return RedirectToAction("test","Login"); //方法名 控制器名 }
删除方法
public ActionResult Delete(int id) { try { Order ord = db.Order.Find(id); //查找对象 db.Order.Remove(ord); //移除对象 db.SaveChanges();//保存 return Content("ok"); } catch (Exception ex) { return Content(ex.Message); } }