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="删除" onclick="Delete(@item.BookID)" />
        @*第二种删除方法*@
            <a href="Book/Delete/@item.BookID" onclick="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; } } }
    (本人微信号:Liberty-bcy)如果,你正在埋怨命运不眷顾,那请记住:命,是失败者的借口;运,是成功者的谦词。
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号