publicActionResult Create () {returnView (); } [httppost] publicactionresult Create (Books book) {book. CreateDate=datetime.now; DBCONTEXT.BOOK.ADD (book); varres =dbcontext.savechanges (); if(res >0) { //return Content ("add success"); returnRedirecttoaction ("Index"); } Else { returnContent ("New Failure"); } } publicActionResult Edit (intId) {varBook =DbContext.Book.Find (id); returnView (book); } [httppost] publicactionresult Edit (Books book) {dbcontext.entry<Books> (book). State =System.Data.Entity.EntityState.Modified; varres =dbcontext.savechanges (); if(res >0) { returnRedirecttoaction ("Index"); } Else returnContent ("modification failed! "); } //[httppost]//public actionresult Edit (Books book)//{ //dbcontext.entry<books> (book). State = System.Data.Entity.EntityState.Modified; //var res = dbcontext.savechanges (); //if (res > 0)// { //return redirecttoaction ("Index"); // } //Else// { //return Content ("modification failed"); // } //} publicActionResult Delete (intId) {//gets the ID of the objectBooks book =NewBooks {Id =id}; Dbcontext.entry<Books> (book). State =System.Data.Entity.EntityState.Deleted; varres =dbcontext.savechanges (); if(res >0) { returnRedirecttoaction ("Index"); } Else returnContent ("Delete Failed"); }
namespace mscampus.mvc.models{[Table ("bookinfos")] public class Books { [Key] [Display (Name = "number")] public int Id {get; set;} [Display (name = "names")] [Column ("bookname")] public string Name {get; set;} [Display (Name = "price")] public decimal Prices {get; set;} [Display (Name = "category")] public string category {get; set;} [Display (Name = "creation time")] public DateTime createdate {get; set;} }}
Declares the EF database context private Mscampusdatamodel dbContext = new Mscampusdatamodel (); Get:book public actionresult Index (string txtname, int pageIndex = 1) { //ef page const int pageSize = 5; ipagedlist<books> Listbook = null; If (!string. IsNullOrEmpty (txtname)) { listbook = dbContext.Book.OrderByDescending (n = N.id) . Where (n = n.name.contains (txtname)) . Topagedlist (pageIndex, pageSize); } else { Listbook = dbContext.Book.OrderByDescending (n = N.id) . Topagedlist (pageIndex, pageSize); } Viewbag.querytitle = txtname; return View (listbook); var list = dbcontext.book; return View (list); }
@model ienumerable<mscampus.mvc.models.books> @using x.pagedlist@using x.pagedlist.mvc@{ViewBag.Title = "Index ";}
Fe+mvc sub-page