ASP.NET.MVC的删除数据

                             ASP.NET.MVC的删除数据  

开发工具与关键技术:Visual Studio 2015
作者:昨夜星辰
撰写时间:2019年04月28日

一、在我们学习ASP.NET.MVC的时候的一个重要的知识点,删除数据,通过删除把一些没有用的数据清除,首先需要通过把需要删除的数据封装起来,创建一个function方法,把需要删除的数据id放进去,然后给用户一个提示是否要删除该信息,防止用户误点,然后用户要是点击确定就继续执行下面的代码打开加载层,发送删除请求可控制器通过id判断需要删除的用户信息,要是判断成功就关闭加载层,判断是否成功,成功就刷新表格数据,最后返回文本内容。
在这里插入图片描述
二、通过HTML传输过来的数据,开始写删除的方法,首先把传过来的数据拼接,然后创建返回的全局变量,然后查询数据里面是否存在有其它的信息,先查询知识点数据,通过上面自命名的变量判断是否等于0,要是不等于0就代表你需要删除的数据里面存在相对应的数据,要是有相对应的数据就else提示一下,该数据存在相对应的数据,不能删除,如果没有就继续执行查询一下需要删除的表格数据,然重model里面把需要删除的数据提取出来,判断如果没有问题就删除成功,否则就删除失败,最后返回页面。
在这里插入图片描述

三、下面截图就是删除的判断代码实现效果图。
在这里插入图片描述 在这里插入图片描述

四、C#的一些基础语法,控制语句)
1、if () … else …(条件语句)
2、switch (多分支选择语句)
3、for()… (循环语句)
4、do…while() (循环语句)
5、while()… (循环语句)达到条件结束
6、break 终止执行switch或循环语句)
7、continue (结束本次循环语句
8、goto (无条件转向语句)
9、return (从函数返回语句)
基础语法:
String 字符型 return 返回 value 接受值
Public 公共 private 私有 && 或者 static 静态(函数)
main(方法) set 写 get 读 Int 数字型 \t 空格 \n 换行
const double p1:const 表明p1是个常量 常量在声明时赋值,之后是不能修改的
double表明p1是个‘双精度浮点型’
常用代码:
输出内容 Console.Write(“Hello World!”); 输出换行内容 Console.WriteLine()
控制台输入 Convert.ToInt32(Console.ReadLine());持续显示 Console.ReadLine();
类型转换 Convert.ToInt32()【转换为整型】 Convert.ToBoolean ()【转换为bit型】
double d = 2.5; int x = (int)d + 1; 转int型
延时代码 using System.Threading; Thread.Sleep(1000);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC 是一种基于模型-视图-控制器 (MVC) 架构的 Web 应用程序框架。它提供了一种创建 Web 应用程序的模式,可以使开发人员将应用程序的逻辑、布局和用户界面分开。 在 ASP.NET MVC 中,常见的增删改查操作可以使用以下步骤完成: 1. 创建模型类:定义要操作的实体类。 2. 创建控制器类:定义处理请求和响应的代码,包括数据操作和视图渲染。 3. 创建视图:定义呈现数据的 HTML 界面。 4. 实现数据操作:可以使用 Entity Framework 或者其他 ORM 框架来实现数据库的增删改查操作。 下面是一个简单的示例,演示如何在 ASP.NET MVC 中实现增删改查操作: 1. 创建模型类: ``` public class Book { public int BookId { get; set; } public string Title { get; set; } public string Author { get; set; } public decimal Price { get; set; } } ``` 2. 创建控制器类: ``` public class BookController : Controller { // 显示书籍列表 public ActionResult Index() { var books = db.Books.ToList(); return View(books); } // 添加书籍 public ActionResult Add() { return View(); } [HttpPost] public ActionResult Add(Book book) { db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } // 修改书籍 public ActionResult Edit(int id) { var book = db.Books.Find(id); return View(book); } [HttpPost] public ActionResult Edit(Book book) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } // 删除书籍 public ActionResult Delete(int id) { var book = db.Books.Find(id); db.Books.Remove(book); db.SaveChanges(); return RedirectToAction("Index"); } private ApplicationDbContext db = new ApplicationDbContext(); } ``` 3. 创建视图: ``` @model IEnumerable<Book> <h2>书籍列表</h2> <p> @Html.ActionLink("添加书籍", "Add") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Author) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Author) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.ActionLink("编辑", "Edit", new { id=item.BookId }) | @Html.ActionLink("删除", "Delete", new { id=item.BookId }) </td> </tr> } </table> ``` ``` @model Book <h2>添加书籍</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="添加" class="btn btn-default" /> </div> </div> } <div> @Html.ActionLink("返回列表", "Index") </div> ``` ``` @model Book <h2>编辑书籍</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="保存" class="btn btn-default" /> </div> </div> } <div> @Html.ActionLink("返回列表", "Index") </div> ``` 4. 实现数据操作: 可以使用 Entity Framework 来实现数据库的增删改查操作,具体代码可以参考以下示例: ``` public class ApplicationDbContext : DbContext { public DbSet<Book> Books { get; set; } } ``` 注意,在使用 Entity Framework 之前,还需要进行一些配置,例如在 Web.config 中添加连接字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值