public ActionResult Create(School school) { //判斷Model驗證結果 if (ModelState.IsValid)//若驗證成功,存入DB { ctx.School.Add(school); ctx.SaveChanges(); return RedirectToAction("Index");//不能用return View("Index"); } return View(school);//若View驗證失敗,顯示之前輸入的結果 }
public ActionResult Edit(School school) { if (ModelState.IsValid) { ctx.Entry(school).State = EntityState.Modified; ctx.SaveChanges(); return RedirectToAction("Index"); } return View(school); }