Esempio n. 1
0
        public ActionResult Edit(Student StuList)
        {
            var urlid = Url.RequestContext.RouteData.Values["id"].ToString();//取出URL上的id
            int id    = Int32.Parse(urlid);

            using (SimpleProjectStudentEntities content = new SimpleProjectStudentEntities())
            {
                if (ModelState.IsValid)
                {
                    Student stuRemoveList = content.Student.FirstOrDefault(m => m.fStudentID == id);
                    content.Student.Remove(stuRemoveList); //這樣只有remove掉Dbset的資料,可以看成移動掉Entity Framework的資料
                    content.SaveChanges();                 //要加上savechanges讓Dbset被更改的資料可以寫入SQL server這樣才有更新資料庫,view就會顯示被更改的資料庫
                    content.Student.Add(StuList);
                    content.SaveChanges();
                }
            }
            return(View("Edit", StuList));
        }
Esempio n. 2
0
 public ActionResult Delete(int iddelete)
 {
     using (SimpleProjectStudentEntities content = new SimpleProjectStudentEntities())
     {
         Student StuList = content.Student.Where(m => m.fStudentID == iddelete).FirstOrDefault();
         content.Student.Remove(StuList);
         content.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
Esempio n. 3
0
 public ActionResult Create(Student StuList)
 {
     using (SimpleProjectStudentEntities content = new SimpleProjectStudentEntities())
     {
         if (ModelState.IsValid)
         {
             content.Student.Add(StuList);
             content.SaveChanges();
             return(RedirectToAction("Index"));//這個加在using內或外有差嗎<=========
         }
         //return RedirectToAction("Index"); 放在這邊會導致下面的return View(Stulist)出現警告 CS0162 偵測到不會執行的程式碼
         //但是VIEW Home//Index一樣可以顯示,資料庫有寫入
     }
     return(View(StuList)); //為什麼一定要加這個,這個加在using內或外有差嗎=>一定要加,加在using沒有用
                            //沒有加的話會出現CS0161 不是所有路徑都有回傳值,實際上StuList不可能全部都有數值,當這個時候沒有辦法回傳
                            //所以要加上return View 強迫StudList有沒有值都要回傳
 }