Exemplo n.º 1
0
        public async Task <IActionResult> Create(TopicAndCourseViewModel model)
        {
            if (ModelState.IsValid)
            {
                var doesTopicExists = _db.Topics.Include(s => s.Course)
                                      .Where(c => c.Name == model.Topic.Name && c.Course.Id == model.Topic.CourseId);

                if (doesTopicExists.Count() > 0)
                {
                    //Error
                    StatusMessage = "Error: Topic exists under " + doesTopicExists.First().Course.Name + " course. Please use another name.";
                }
                else
                {
                    _db.Topics.Add(model.Topic);
                    await _db.SaveChangesAsync();

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

            TopicAndCourseViewModel modelVM = new TopicAndCourseViewModel()
            {
                CourseList    = await _db.Courses.ToListAsync(),
                Topic         = model.Topic,
                TopicList     = await _db.Topics.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(),
                StatusMessage = StatusMessage
            };

            return(View(modelVM));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create()
        {
            TopicAndCourseViewModel model = new TopicAndCourseViewModel()
            {
                CourseList = await _db.Courses.ToListAsync(),
                Topic      = new Models.Topic(),
                TopicList  = await _db.Topics.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync()
            };

            return(View(model));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var topic = await _db.Topics.SingleOrDefaultAsync(m => m.Id == id);

            if (topic == null)
            {
                return(NotFound());
            }

            TopicAndCourseViewModel model = new TopicAndCourseViewModel()
            {
                CourseList = await _db.Courses.ToListAsync(),
                Topic      = topic,
                TopicList  = await _db.Topics.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync()
            };

            return(View(model));
        }