MVC Controller操作 CRUD

2011-12-20 22:40 1824人阅读 评论(0) 收藏 举报
 分类: asp.net MVC(6)  
版权声明:本文为博主原创文章,未经博主允许不得转载。

 
Controller操作
主要简单备忘增、删、查、改的Controller一般操作方法,操作对象为Students实体、context为上下文连接
students对象包括name,age,sex信息,操作页面都是在MVC3中使用强类型、Razor模版建立的。
1、定义查询Index
[csharp] view plain copy
public ActionResult Index()  
{  
   var list = context.Students.ToList(); // 获取students对象信息  
   return View(list); // 返回list数据给Index界面   
 }  


2、  定义添加Controller
    // GET: /Student/Create
 // 用于显示添加界面
[csharp] view plain copy
public ActionResult Create()  
{  
 return View(); // 默认视图页面为Crete.cshtml  
}   


   定义添加操作Action
  [HttpPost] // 必须跟上表示Post请求执行submit按钮提交
[csharp] view plain copy
public ActionResult Create(Student student)  
        {  
            try  
            {  
                // TODO: Add insert logic here  
                if (ModelState.IsValid)  
                {                 
       
                    context.Students.Add(student); // 附加对象student  
       
                    context.SaveChanges(); // 执行持久化操作  
       
                    return RedirectToAction("Index"); // 返回到Index页面  
                }  
                  
            }  
            catch  
            {  
               // 异常信息  
  
            }  
            return View(student);  
        }  
    

 

3、定义修改Controller
 // 获取要修改的页面信息 默认页面为Edit.cshtml
[csharp] view plain copy
public ActionResult Edit(int id)  
{  
   var model = context.Students.Find(id); // 根据ID查询获取修改信息  
    return View(model); // 并赋值给View页面  
}  

 // 执行编辑操作
[csharp] view plain copy
[HttpPost]  
       public ActionResult Edit(Student  student)  
       {  
        
               // TODO: Add update logic here  
  
               if (ModelState.IsValid)  
               {  
       // 会自动识别哪个属性被修改  
                   context.Entry(student).State = EntityState.Modified; // 标志为修改状态Modifyed,表示要修改,Detached、Added、Unchanged、Modifyed、Deleted  
                   int i = context.SaveChanges();  
                   return RedirectToAction("Index"); // 修改成功返回首页  
               }  
           return View(student);  
       }  

 

4、定义删除Controller
 

[csharp] view plain copy
// 获取要删除的信息 默认页面为delete.cshtml  
    public ActionResult Delete(int id)  
       {  
           var model = context.Students.Find(id);  
           return View(model);  
       }  
 // 执行操作方法  
    [ActionName("Delete")] // 这里被定义了两个一样的Delete,所以需要用ActionName特性指定个Delete的Action  
       [HttpPost]  
       public ActionResult DeletePost(int id) // 定义成DeletePost,否则提示错误  
       {  
           try  
           {  
               // TODO: Add delete logic here  
                
               var student = context.Students.Find(id);  
                
               context.Students.Remove(student); // 移除操作  
               // 变成Deleted状态  
               context.SaveChanges(); // 持久化  
               return RedirectToAction("Index");  
           }  
           catch  
           {  
               return View();  
           }  
       }  
   
 利用Ajax删除,先修改Controller代码:  
  try  
           {  
               // TODO: Add delete logic here  
               if (Request.IsAjaxRequest())  
               {  
                   var student = context.Students.Find(id);  
                   context.Students.Remove(student);  
                   int k = context.SaveChanges();  
                   return Content(k.ToString());  
               }  
               else  
               {  
                   return Content("-1"); // 返回内容为-1 表示删除失败  
               }  
           }  
           catch  
           {  
               return Content("-1");  
           }  

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

[csharp] view plain copy
<a href="#" name="delete" sid=@item.StudentID>删除</a>  
  用jquery删除  
  <script type="text/javascript">  
$().ready(function () {  
      $("[name='delete']").click(function () {  
          if (confirm("确定删除信息?")) {  
              var sid = $(this).attr("sid");  
  
              var trContent = $(this).parent().parent();  
              $.post("Student/Delete/", { id: sid }, function (data) {  
    if (data == "-1") {  
     alert("删除失败");  
    }  
    else {  
     $(trContent).remove();  
     alert("删除成功");  
    }  
  
  
   })  
  }  
 })  
})  
</script>  

 

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

  

 1824人阅读 评论(0) 收藏 举报
 分类:
 

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

[csharp] view plain copy
 
  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 plain copy
 
  1. public ActionResult Create()  
  2. {  
  3.  return View(); // 默认视图页面为Crete.cshtml  
  4. }   



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

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


 // 执行编辑操作

[csharp] view plain copy
 
  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 plain copy
 
  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 plain copy
 
  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操作
 

原文地址:https://www.cnblogs.com/ruishuang208/p/5976660.html