Example #1
0
        public ActionResult Contact()
        {
            ViewBag.Message = "與我們聯絡";

            /*
            List<string> DepartmentList = new List<string>();
            DepartmentList.Add("業務部");
            DepartmentList.Add("創意部");
            DepartmentList.Add("行銷部");

            ViewBag.DepartmentList = DepartmentList;

            return View();

            */

            List<department> departmentlist = new List<department>();

            SqlHelper hp = new SqlHelper();

            DataTable tb = hp.Sql2Table("select id,cname from dep");

            department depobj;
            foreach(DataRow r in tb.Rows)
            {
                depobj = new department();
                depobj.id = r["id"].ToString();
                depobj.departmentName = r["cname"].ToString();
                departmentlist.Add(depobj);
            }

            //return View(departmentlist);

            departmentlist.Clear();
            depobj = new department();
            depobj.id = "a001"; depobj.departmentName = "業務部";
            departmentlist.Add(depobj);

            depobj = new department();
            depobj.id = "a002"; depobj.departmentName = "創意部";
            departmentlist.Add(depobj);

            depobj = new department();
            depobj.id = "a003"; depobj.departmentName = "行銷部";
            departmentlist.Add(depobj);

            return View(departmentlist);
        }
        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);
            }
        }
        // GET: Department
        public ActionResult Index(string departmentid)
        {
            //ViewBag.departmentid = departmentid;
            SqlHelper hp = new SqlHelper();
            DataTable tb = hp.Sql2Table("select id,cname from dep where id=" + departmentid);
            department depobj = new department();

            if (tb.Rows.Count==1)
            {
                depobj.id = tb.Rows[0]["id"].ToString();
                depobj.departmentName = tb.Rows[0]["cname"].ToString();
            }
            else
            {
                ModelState.AddModelError("","無此編號的部門存在");
            }
            return View(depobj);
        }
        public ActionResult Edit(department model)
        {
            SqlHelper hp = new SqlHelper();
            hp.execsql("update dep set cname='" + model.departmentName + "' where id=" + model.id);
            //return View(model);

            return RedirectToAction("Department", "Admin", null);
        }