zoukankan      html  css  js  c++  java
  • ASP.NET MVC删除数据

    Index.cshtml

    @model IEnumerable<MvcExample.Models.Category>
    <script type="text/javascript">
        function Delete(categoryID) {
            if (confirm("确定要删除?")) {
                url = "/Category/Delete";
                parameter = { id: categoryID };
                $.post(url, parameter, function (data) {
                    alert("删除成功!");
                    window.location = "/Category";
                });
            }
        }
    </script>
    <table>
        <tr class="title">
            <td>
                名称
            </td>
            <td>
                操作
            </td>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.CategoryName
                </td>
                <td>
                    <input type="button" onclick="Delete(@item.CategoryID)" text="删除"/>
                </td>
            </tr>
        }
    </table>

    CategoryController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    using System.Data.Entity;
    
    using MvcExample.Models;
    
    namespace MvcExample.Controllers
    {
        public class CategoryController : Controller
        {
            private MvcExampleContext ctx = new MvcExampleContext();
    
            public ActionResult Index()
            {
                return View(ctx.Categories.ToList());
            }
    
            [HttpPost]
            public ActionResult Delete(int id)
            {
                Category category = ctx.Categories.Find(id);
                ctx.Categories.Remove(category);
                ctx.SaveChanges();
                return RedirectToAction("Index");
            }
    
            protected override void Dispose(bool disposing)
            {
                ctx.Dispose();
                base.Dispose(disposing);
            }
        }
    }
  • 相关阅读:
    CCS样式命名
    BFC机制
    html及css小结
    盒模型
    C#函数的使用方法
    如何读代码
    利用CSS-border属性实现圆饼图表
    WNMP环境搭建(win10+Ndinx1.9.15+MySQL5.7.12+PHP5.6.21)
    vue 项目优化:引入cdn使用插件, 减少打包体积
    'PORT' 不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/libingql/p/3493226.html
Copyright © 2011-2022 走看看