MVC3.0删除数据的时候给提示信息
生活随笔
收集整理的這篇文章主要介紹了
MVC3.0删除数据的时候给提示信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Index.cshtml代碼:
@model IEnumerable<FirstMvc.Models.Book><script type="text/javascript">function Delete(bkid) {if (confirm("確定刪除數據嗎?")) {//刪除的時候給提示url = "/Book/Delete";parameter = { id: bkid };$.post(url, parameter, function (data) {window.location = "/Book";});}}</script>@{ViewBag.Title = "首頁"; }<h2>圖書管理</h2><p>@Html.ActionLink("增加圖書", "Create") </p> <table><tr><th>圖書名稱</th><th>作者</th><th>出版社</th><th>價格</th><th>備注</th><th></th></tr>@foreach (var item in Model) {<tr><td>@Html.DisplayFor(modelItem => item.BookName)</td><td>@Html.DisplayFor(modelItem => item.Author)</td><td>@Html.DisplayFor(modelItem => item.Publisher)</td><td>@Html.DisplayFor(modelItem => item.Price)</td><td>@Html.DisplayFor(modelItem => item.Remark)</td><td><input type="button" value="刪除" οnclick="Delete(@item.BookID)" />@*第二種刪除方法*@
?????????? <a href="Book/Delete/@item.BookID" οnclick="return confirm('確定刪除數據?')">刪除</a></td><td>@Html.ActionLink("編輯", "Edit", new { id = item.BookID }) |@Html.ActionLink("查看", "Details", new { id = item.BookID }) |@Html.ActionLink("刪除", "Delete", new { id = item.BookID })</td></tr> }</table>
BookController代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using FirstMvc.Models;namespace FirstMvc.Controllers {public class BookController : Controller{//// GET: /Book/BookDbContext db = new BookDbContext();public ActionResult Index(){return View(db.Books.ToList());}public ActionResult Create(Book book){if (ModelState.IsValid){db.Books.Add(book);db.SaveChanges();return RedirectToAction("Index");}else{RedirectToAction("Index");}return View();}//刪除數據public ActionResult Delete(int id){Book book = db.Books.Find(id); db.Books.Remove(book); db.SaveChanges();return RedirectToAction("Index");}} }
Book.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations;namespace FirstMvc.Models {public class Book{public int BookID { get; set; }[Required(ErrorMessage = "必須輸入圖書名稱")]public string BookName { get; set; }[Required(ErrorMessage = "必須輸入作者名稱")]public string Author { get; set; }[Required(ErrorMessage = "必須輸入出版社")]public string Publisher { get; set; }public decimal Price { get; set; }public string Remark { get; set; }} }BookDbContext.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity;namespace FirstMvc.Models {public class BookDbContext:DbContext{public DbSet<Book> Books { get; set; }} }?
轉載于:https://www.cnblogs.com/LoveQin/p/4692621.html
總結
以上是生活随笔為你收集整理的MVC3.0删除数据的时候给提示信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: str.length() 与 str.g
- 下一篇: 让后台服务不被杀———电话录音