FE+MVC分页

本文介绍了一个基于ASP.NET MVC的应用程序实现书籍信息的增删改查(CRUD)操作,包括创建(Create)、读取(Retrieve)、更新(Update)和删除(Delete)功能,并展示了如何使用Entity Framework进行数据库交互。
 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>

  

  

转载于:https://www.cnblogs.com/dongwenfei/p/6371908.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值