Exemplo n.º 1
0
        public async Task <ActionResult> Create(Meeting meeting)
        {
            if (ModelState.IsValidField("MeetingName") &&
                ModelState.IsValidField("NumberOfParticipants") &&
                ModelState.IsValidField("RoomID") &&
                ModelState.IsValidField("StartTime") &&
                ModelState.IsValidField("EndTime"))
            {
                // 检查会议室是否可用
                string errorMessage;
                bool   result = ReservationRules.CheckRoom(meeting, out errorMessage);
                if (!result)
                {
                    ModelState.AddModelError("", errorMessage);
                }
                else
                {
                    // 添加参会人员
                    if (!string.IsNullOrEmpty(Request.Form["Participants"]))
                    {
                        string[] ids = Request.Form["Participants"].Split(',');
                        foreach (string id in ids)
                        {
                            meeting.Participants.Add(db.Employees.Find(int.Parse(id)));
                        }
                    }

                    // 预定者编号
                    Employee reservationist = db.Employees.First(emp => emp.UserName == User.Identity.Name);
                    meeting.ReservationistID = reservationist.EmployeeID;

                    db.Meetings.Add(meeting);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.Rooms = new SelectList(
                db.Rooms.Where(r => r.RoomStatus == RoomStatusType.Active),
                "RoomID", "RoomCode", meeting.RoomID);
            ViewBag.Departments = new SelectList(db.Departments, "DepartmentID", "DepartmentName");

            return(View(meeting));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create([Bind(Include = "RoomID,RoomCode,RoomName,Capacity,Descrption")] Room room)
        {
            if (ModelState.IsValid)
            {
                var existed = db.Rooms.FirstOrDefault(r => r.RoomStatus != RoomStatusType.Retired &&
                                                      (r.RoomCode == room.RoomCode || r.RoomName == room.RoomName));
                if (existed != null)
                {
                    ModelState.AddModelError("opresult", "会议室编号或名称已经存在!");
                }
                else
                {
                    db.Rooms.Add(room);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(room));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Create(
            [Bind(Include = "DepartmentID, DepartmentName")]
            Department department)
        {
            if (ModelState.IsValid)
            {
                Department existed = await db.Departments.FirstOrDefaultAsync(d =>
                                                                              string.Compare(d.DepartmentName, department.DepartmentName, true) == 0);

                if (existed != null)
                {
                    ModelState.AddModelError("opresult", "部门名称已经存在!");
                }
                else
                {
                    db.Departments.Add(department);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(department));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Edit(
            [Bind(Include = "EmployeeID,EmployeeName,Gender,DepartmentID")] Employee employee,
            string returl)
        {
            // 因为UserName, Email, Password等字段并不在Edit页面中出现,因此在验证模型时,仅检查下列三个字段
            if (ModelState.IsValidField("EmployeeName") &&
                ModelState.IsValidField("Gender") &&
                ModelState.IsValidField("DepartmentID"))
            {
                // Employee表中实际上不需要Password列,因此employee.Password将为null
                // 但是Password加了Required验证,在SaveChange时会导致验证失败
                // 设置ValidateOnSaveEnabled为false,将禁用验证
                db.Configuration.ValidateOnSaveEnabled = false;
                var emp = await db.Employees.FindAsync(employee.EmployeeID);

                emp.EmployeeName = employee.EmployeeName;
                emp.Gender       = employee.Gender;
                emp.DepartmentID = employee.DepartmentID;
                await db.SaveChangesAsync();

                // 更新图片
                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    file.SaveAs(System.IO.Path.Combine(Server.MapPath("/Photos"), emp.PhotoUrl));
                }

                if (!string.IsNullOrEmpty(returl))
                {
                    return(Redirect(returl));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
            ViewBag.Departments = new SelectList(db.Departments, "DepartmentID", "DepartmentName", employee.DepartmentID);
            return(View(employee));
        }