• 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; }
        }
    }
  • 相关阅读:
    【Java123】javapoet代码生成代码工具
    【Python123】OptionParser初使用
    【Spring123】JdbcTemplate初使用 以及 ORA-01858: a non-numeric character was found where a numeric was expected, ORA-00911: invalid character解决
    【Java123】解决javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    git常用命令
    在虚拟机上搭建自己的 git 服务器并创建 git 仓库
    git用法
    Golang gRPC框架3-自签证书验证
    go _nsq
    mysql的备份和恢复
  • 原文地址:https://www.cnblogs.com/LoveQin/p/4692621.html
Copyright © 2020-2023  润新知