• 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; }
        }
    }
  • 相关阅读:
    python 装饰器
    git
    JS原生方法实现jQuery的ready()
    js获取css属性方法
    列表页调出点击量
    数组操作
    判断IE版本
    判断IE浏览器用IE条件表达式
    [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
    复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html
  • 原文地址:https://www.cnblogs.com/LoveQin/p/4692621.html
Copyright © 2020-2023  润新知