Exemplo n.º 1
0
        public ActionResult Create(staff staff, AddressViewModel address)
        {
            if (ModelState.IsValid)
            {
                //Save infor staff
                var code = db.staffs.OrderByDescending(x => x.sta_id).FirstOrDefault();
                if (code == null)
                {
                    staff.sta_code = "NV000001";
                }
                else
                {
                    staff.sta_code = Utilis.CreateCodeByCode("NV", code.sta_code, 8);
                }
                staff.sta_created_date = DateTime.Now;
                staff.sta_status       = 1;
                //Mail
                string content = System.IO.File.ReadAllText("D:/Ki2Nam3/ttn/Tien_TTNhom/CodeAllTTNhom/Common/Template/sendmail.html");
                content = content.Replace("{{Username}}", staff.sta_username);
                content = content.Replace("{{Password}}", staff.sta_password);

                new MailHelper().SendMail(staff.sta_email, "Thông tin tài khoản", content);
                db.staffs.Add(staff);

                db.SaveChanges();
                //Save address of staff

                staff   staff_last = db.staffs.OrderByDescending(x => x.sta_id).FirstOrDefault();
                address create_add = new address();
                create_add.add_province = db.Provinces.Where(x => x.Id == address.provinceID).FirstOrDefault().Name;
                create_add.add_district = db.Districts.Where(x => x.Id == address.districtID).FirstOrDefault().Name;
                create_add.add_ward     = db.Wards.Where(x => x.Id == address.wardID).FirstOrDefault().Name;
                create_add.staff_id     = staff_last.sta_id;
                db.addresses.Add(create_add);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.department_id = new SelectList(db.departments, "de_id", "de_name", staff.department_id);
            ViewBag.group_role_id = new SelectList(db.group_role, "gr_id", "gr_name", staff.group_role_id);
            ViewBag.position_id   = new SelectList(db.positions, "pos_id", "pos_name", staff.position_id);

            ViewBag.provinceID = new SelectList(db.Provinces, "Id", "Name", address.provinceID);
            ViewBag.districtID = new SelectList(db.Districts, "Id", "Name", address.districtID);
            ViewBag.wardID     = new SelectList(db.Wards, "Id", "Name", address.wardID);
            return(View(staff));
        }