public ActionResult Create(department model) { SqlHelper hp = new SqlHelper(); int icount = 0; icount=Convert.ToInt32(hp.sql2result("select count(id) from dep where id=" + model.id)); if (icount>0) { ModelState.AddModelError("", "部門編號"+model.id+"已經存在"); } icount = Convert.ToInt32(hp.sql2result("select count(cname) from dep where cname='" + model.departmentName+"'")); if (icount > 0) { ModelState.AddModelError("", "部門名稱" + model.departmentName + "已經存在"); } /* if (model.departmentName=="業務部") { ModelState.AddModelError("", "該部門名稱已經存在"); } */ if (ModelState.IsValid) { //寫入資料庫的程式碼 hp.sql2result( string.Format("insert into dep (id,cname) values ({0},'{1}')", model.id,model.departmentName ) ); // return RedirectToAction("Department", "admin"); } else { return View(model); } }