public ActionResult Index(string id)//主页 //参数string searchString 访问方式为index?searchString=xxxx 。参数string id 访问方式为index/x { string searchString = id; //return View(db.Books.ToList()); //返回一个对象集合 var s = from m in db.Books select m; //查询所有数据 if (!string.IsNullOrEmpty(searchString)) //判断传来的数据是否位空 { s = s.Where(x => x.BookName.Contains(searchString)); //模糊查询数据 } return View(s); }
public ActionResult Edit(int? id) //只能接受整型数据;其他默认null { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest);//传递过去400 //返回400页面 } Book book = db.Books.Find(id); //在books表中查找指定id的对象 赋值给Book对象 if (book == null) { return HttpNotFound(); //未找到调用HttpNotFound()方法,传递 NotFound = 404,返回404 页面 } return View(book); //返回这个对象 }