asp.net MVC3 中Conroller中对于CRUD的基本操作

来源:互联网 发布:手机金属探测仪软件 编辑:程序博客网 时间:2024/06/04 00:43

Controller操作
主要简单备忘增、删、查、改的Controller一般操作方法,操作对象为Students实体、context为上下文连接
students对象包括name,age,sex信息,操作页面都是在MVC3中使用强类型、Razor模版建立的。
1、定义查询Index

[csharp] view plaincopy
  1. public ActionResult Index()  
  2. {  
  3.    var list = context.Students.ToList(); // 获取students对象信息  
  4.    return View(list); // 返回list数据给Index界面   
  5.  }  



2、  定义添加Controller
    // GET: /Student/Create
 // 用于显示添加界面

[csharp] view plaincopy
  1. public ActionResult Create()  
  2. {  
  3.  return View(); // 默认视图页面为Crete.cshtml  
  4. }   



   定义添加操作Action
  [HttpPost] // 必须跟上表示Post请求执行submit按钮提交

[csharp] view plaincopy
  1. public ActionResult Create(Student student)  
  2.         {  
  3.             try  
  4.             {  
  5.                 // TODO: Add insert logic here  
  6.                 if (ModelState.IsValid)  
  7.                 {                 
  8.        
  9.                     context.Students.Add(student); // 附加对象student  
  10.        
  11.                     context.SaveChanges(); // 执行持久化操作  
  12.        
  13.                     return RedirectToAction("Index"); // 返回到Index页面  
  14.                 }  
  15.                   
  16.             }  
  17.             catch  
  18.             {  
  19.                // 异常信息  
  20.   
  21.             }  
  22.             return View(student);  
  23.         }  
  24.     


 

3、定义修改Controller
 // 获取要修改的页面信息 默认页面为Edit.cshtml

[csharp] view plaincopy
  1. public ActionResult Edit(int id)  
  2. {  
  3.    var model = context.Students.Find(id); // 根据ID查询获取修改信息  
  4.     return View(model); // 并赋值给View页面  
  5. }  


 // 执行编辑操作

[csharp] view plaincopy
  1. [HttpPost]  
  2.        public ActionResult Edit(Student  student)  
  3.        {  
  4.         
  5.                // TODO: Add update logic here  
  6.   
  7.                if (ModelState.IsValid)  
  8.                {  
  9.        // 会自动识别哪个属性被修改  
  10.                    context.Entry(student).State = EntityState.Modified; // 标志为修改状态Modifyed,表示要修改,Detached、Added、Unchanged、Modifyed、Deleted  
  11.                    int i = context.SaveChanges();  
  12.                    return RedirectToAction("Index"); // 修改成功返回首页  
  13.                }  
  14.            return View(student);  
  15.        }  


 

4、定义删除Controller
 

[csharp] view plaincopy
  1. // 获取要删除的信息 默认页面为delete.cshtml  
  2.     public ActionResult Delete(int id)  
  3.        {  
  4.            var model = context.Students.Find(id);  
  5.            return View(model);  
  6.        }  
  7.  // 执行操作方法  
  8.     [ActionName("Delete")] // 这里被定义了两个一样的Delete,所以需要用ActionName特性指定个Delete的Action  
  9.        [HttpPost]  
  10.        public ActionResult DeletePost(int id) // 定义成DeletePost,否则提示错误  
  11.        {  
  12.            try  
  13.            {  
  14.                // TODO: Add delete logic here  
  15.                 
  16.                var student = context.Students.Find(id);  
  17.                 
  18.                context.Students.Remove(student); // 移除操作  
  19.                // 变成Deleted状态  
  20.                context.SaveChanges(); // 持久化  
  21.                return RedirectToAction("Index");  
  22.            }  
  23.            catch  
  24.            {  
  25.                return View();  
  26.            }  
  27.        }  
  28.    
  29.  利用Ajax删除,先修改Controller代码:  
  30.   try  
  31.            {  
  32.                // TODO: Add delete logic here  
  33.                if (Request.IsAjaxRequest())  
  34.                {  
  35.                    var student = context.Students.Find(id);  
  36.                    context.Students.Remove(student);  
  37.                    int k = context.SaveChanges();  
  38.                    return Content(k.ToString());  
  39.                }  
  40.                else  
  41.                {  
  42.                    return Content("-1"); // 返回内容为-1 表示删除失败  
  43.                }  
  44.            }  
  45.            catch  
  46.            {  
  47.                return Content("-1");  
  48.            }  


   
   修改查询的页面中删除的链接、
   把原来的 @Html.ActionLink("删除", "Delete", new { id=item.StudentID })
   换成
  

[csharp] view plaincopy
  1. <a href="#" name="delete" sid=@item.StudentID>删除</a>  
  2.   用jquery删除  
  3.   <script type="text/javascript">  
  4. $().ready(function () {  
  5.       $("[name='delete']").click(function () {  
  6.           if (confirm("确定删除信息?")) {  
  7.               var sid = $(this).attr("sid");  
  8.   
  9.               var trContent = $(this).parent().parent();  
  10.               $.post("Student/Delete/", { id: sid }, function (data) {  
  11.     if (data == "-1") {  
  12.      alert("删除失败");  
  13.     }  
  14.     else {  
  15.      $(trContent).remove();  
  16.      alert("删除成功");  
  17.     }  
  18.   
  19.   
  20.    })  
  21.   }  
  22.  })  
  23. })  
  24. </script>  


 

 在学习的过程中主要记录下asp.net MVC 的基本CRUD操作

0 0
原创粉丝点击