標籤: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分頁