コード例 #1
0
        public ActionResult Create(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
            return View(employee);
        }
コード例 #2
0
        public ActionResult Create(Employee employee)
        {
            if(string.IsNullOrEmpty(employee.Name))
            {
                ModelState.AddModelError("Name", "The Name Field Required");
            }

            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
            return View(employee);
        }
コード例 #3
0
 public ActionResult Edit(Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
     return View(employee);
 }