public ActionResult Create(Models.Daily postback) // --Create POST-- { using (Models.MymoneyEntities db = new Models.MymoneyEntities()) { db.Mymoney.Add(postback); //將回傳資料postback,加入至Mymoney DB TempData["ResultMessage"] = String.Format("紀錄[{0}]成功建立", postback.Item); //設定成功訊息 db.SaveChanges(); //儲存資料 } return(RedirectToAction("Index")); }
public ActionResult Edit(Models.Daily postback) // --編輯頁 POST-- { if (this.ModelState.IsValid) //判斷使用者輸入資料是否正確 { using (Models.MymoneyEntities db = new Models.MymoneyEntities()) { var result = (from s in db.Mymoney where s.Id == postback.Id select s).FirstOrDefault(); //抓取Mymoney.Id,等於回傳postback.Id的資料 //儲存使用者變更資料 result.Item = postback.Item; result.Price = postback.Price; result.Payment = postback.Payment; db.SaveChanges(); //儲存 TempData["ResultMessage"] = String.Format("紀錄[{0}]成功編輯", postback.Item); //成功訊息,導回index頁 return(RedirectToAction("Index")); } } else //如果資料不正確,導向自己(Edit頁) { return(View(postback)); } }