FE+MVC分頁

來源:互聯網
上載者:User

標籤:put   rem   enum   時間   textbox   image   ons   get   rect   

 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分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.