コード例 #1
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                var employee = new LMEmployee()
                {
                    EmpId              = Convert.ToInt32(collection.Get("EmpId")),
                    EmpFName           = collection.Get("EmpFName"),
                    EmpLName           = collection.Get("EmpLName"),
                    DateOfBirth        = Convert.ToDateTime(collection.Get("DateOfBirth")),
                    ManagerEmpId       = Convert.ToInt32(collection.Get("ManagerEmpId")),
                    Grade              = collection.Get("Grade"),
                    Sex                = collection.Get("Sex"),
                    Tel                = collection.Get("Tel"),
                    SickLeaveBalance   = Convert.ToInt32(collection.Get("SickLeaveBalance")),
                    EarnedLeaveBalance = Convert.ToInt32(collection.Get("EarnedLeaveBalance")),
                    Email              = collection.Get("Email"),
                    Pass               = collection.Get("Pass")
                };

                //context.Add(employee);
                context.LMEmployees.Add(employee);
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
コード例 #2
0
        public int InsertEmployee(LMEmployee employee)
        {
            int result = -1;

            try {
                Employees.AddOrUpdate(employee);
                result = this.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }

                throw;
            }

            if (result != -1)
            {
                result = 1;
            }

            return(result);
        }
コード例 #3
0
        public ActionResult Create(FormCollection collection)
        {
            if (Request.HttpMethod == "POST")
            {
                //int checkEmployee = 0;
                var empId         = Convert.ToInt32(collection.Get("EmpId"));
                var checkEmployee = _context.Employees.Where(e => e.EmpId == empId).FirstOrDefault();

                if (checkEmployee != null)
                {
                    ViewData["msg"] = "The employee with the same id exist.";
                    return(View());
                }

                var employee = new LMEmployee()
                {
                    EmpId              = Convert.ToInt32(collection.Get("EmpId")),
                    EmpFName           = collection.Get("EmpFName"),
                    EmpLName           = collection.Get("EmpLName"),
                    DateOfBirth        = Convert.ToDateTime(collection.Get("DateOfBirth")),
                    ManagerEmpId       = Convert.ToInt32(collection.Get("ManagerEmpId")),
                    Grade              = collection.Get("Grade"),
                    Sex                = collection.Get("Sex"),
                    Tel                = collection.Get("Tel"),
                    SickLeaveBalance   = Convert.ToInt32(collection.Get("SickLeaveBalance")),
                    EarnedLeaveBalance = Convert.ToInt32(collection.Get("EarnedLeaveBalance")),
                    Email              = collection.Get("Email"),
                    Pass               = collection.Get("Pass")
                };

                //context.Add(employee);
                //_context.Employees.Add(employee);
                //int result =0;

                //result = _context.SaveChanges(); //le resultat vas etre 1 si le requete executé avec le successé.
                var result = _context.InsertEmployee(employee);
                //ntext.SaveChanges();


                if (result == 1)
                {
                    ViewData["msg"] = "The employee created successfully.";
                    return(View());
                }
            }

            return(View());
        }