Asp.Net中MVC删除数据技巧
分类:.Net开发
作者:MVC
阅读:18881
Asp.Net中MVC删除数据技巧
Index.cshtml页面
01 | @model IEnumerable< MvcExample.Models.Category > |
02 | < script type = "text/javascript" > |
03 | function Delete(categoryID) { |
04 | if (confirm("确定要删除?")) { |
05 | url = "/Category/Delete"; |
06 | parameter = { id: categoryID }; |
07 | $.post(url, parameter, function (data) { |
09 | window.location = "/Category"; |
23 | @foreach (var item in Model) |
30 | < input type = "button" onclick = "Delete(@item.CategoryID)" text = "删除" /> |
CategoryController.cs 控制器
02 | using System.Collections.Generic; |
07 | using System.Data.Entity; |
09 | using MvcExample.Models; |
11 | namespace MvcExample.Controllers |
13 | public class CategoryController : Controller |
15 | private MvcExampleContext ctx = new MvcExampleContext(); |
17 | public ActionResult Index() |
19 | return View(ctx.Categories.ToList()); |
23 | public ActionResult Delete( int id) |
25 | Category category = ctx.Categories.Find(id); |
26 | ctx.Categories.Remove(category); |
28 | return RedirectToAction( "Index" ); |
31 | protected override void Dispose( bool disposing) |
34 | base .Dispose(disposing); |
原文链接:Asp.Net中MVC删除数据技巧
版权所属: www.jsons.cn
本文链接: http://www.jsons.cn/articleinfo/235/
特别声明:如需转载,请以链接形式注明本文出处!