Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("RealName,HrieDate,OfficeAssignment")] Instructor instructor
                                                 , string[] select)
        {
            if (select != null)
            {
                instructor.courseAssignments = new List <CourseAssignment>();
                foreach (var item in select)
                {
                    var CourseAdd = new CourseAssignment()
                    {
                        CourseId     = Convert.ToInt32(item),
                        InstructorId = instructor.Id
                    };
                    instructor.courseAssignments.Add(CourseAdd);
                }
            }

            if (ModelState.IsValid)
            {
                _context.Add(instructor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            PopulateAssignedCourseData(instructor);
            return(View(instructor));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(StudentDto Dto)
        {
            try
            {
                //数据验证,判断数据是否符合特性要求,比如tdo模型中特性要求某个属性不能为空等
                if (ModelState.IsValid)
                {
                    //从tdo模型绑定到model
                    var entity = new Student()
                    {
                        RealName       = Dto.RealName,
                        EnrollmentDate = Dto.EnrollmentDate
                    };
                    _context.Add(entity);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException ex)
            {
                //给数据模型添加一个错误信息可以在页面中显示
                ModelState.AddModelError("", "无法保存,请检查输入的数据!");
            }


            return(View(Dto));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("CourseId,Title,Credits,Grade,DepartmentId")] Course course)
        {
            if (ModelState.IsValid)
            {
                _context.Add(course);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(course));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,name,Budget,StarDate,InstructorId,RowVersion")] Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["InstructorId"] = new SelectList(_context.Instructor, "Id", "Id", department.InstructorId);
            return(View(department));
        }