首页 > 代码库 > FE+MVC分页

FE+MVC分页

 public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Books book)
        {
            book.CreateDate = DateTime.Now;
            dbContext.Book.Add(book);
            var res = dbContext.SaveChanges();
            if (res > 0)
            {
                //return Content("添加成功");
                return RedirectToAction("Index");
            }
            else
            {
                return Content("新增失败");
            }
        }

        public ActionResult Edit(int id)
        {
            var book = dbContext.Book.Find(id);
            return View(book);
        }

        [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("修改失败!");
        }

        //[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("修改失败");
        //    }
        //}

        public ActionResult Delete(int id)
        {
            //获取Id的对象
            Books book = new Books { Id = id };
            dbContext.Entry<Books>(book).State = System.Data.Entity.EntityState.Deleted;
            var res = dbContext.SaveChanges();
            if (res > 0)
            {
                return RedirectToAction("Index");
            }
            else
                return Content("删除失败");

        }

 

技术分享

namespace MSCampus.MVC.Models
{
    [Table("BookInfos")]
    public class Books
    {
        [Key]
        [Display(Name = "编号")]
        public int Id { get; set; }

        [Display(Name = "名称")]
        [Column("BookName")]
        public string Name { get; set; }

        [Display(Name = "价格")]
        public decimal Price { get; set; }

        [Display(Name = "分类")]
        public string Category { get; set; }

        [Display(Name = "创建时间")]
        public DateTime CreateDate { get; set; }



    }
}
//声明EF数据库上下文
        private MSCampusDataModel dbContext = new MSCampusDataModel();

        // GET: Book
        public ActionResult Index(string txtName, int pageIndex = 1)
        {
            //EF分页
            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";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Index", "Book"))
{
    <div class="form-inline" style="margin:20px;">
        <div class="form-group">
            <label for="inputPassword2" class="sr-only">Title</label>
            @Html.TextBox("txtName", ViewData["QueryTitle"], new { @class = "form-control", placeholder = "标题" })
        </div>
        <button type="submit" class="btn btn-primary">查 询</button>
    </div>

}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CreateDate)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Category)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreateDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }
</table>

<div style="text-align:center;">
    @Html.PagedListPager((IPagedList)Model, pageIndex => (Url.Action("Index", new { pageIndex, txtTitle = ViewData["QueryTitle"] })))
</div>

  

  

FE+MVC分页