Пример #1
0
        public IActionResult AddSection(SectionCreateVM model)
        {
            int size = _context.Sections.Where(x => x.CourseId == model.CourseId && x.ParentSectionId == model.ParentSectionId).Count();

            if (ModelState.IsValid)
            {
                if (model.Id > 0)
                {
                    var entity = _context.Sections.FirstOrDefault(x => x.Id == model.Id);
                    if (entity != default)
                    {
                        entity.Name = model.Name;
                        _context.Update(entity);
                    }
                }
                else
                {
                    var entity = new SectionModel
                    {
                        Id              = model.Id,
                        CourseId        = model.CourseId,
                        Name            = model.Name,
                        ParentSectionId = model.ParentSectionId,
                        DisplayOrder    = size + 1
                    };
                    _context.Add(entity);
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Edit), new { id = model.CourseId }));
            }
            else
            {
                return(View(model));
            }
        }
Пример #2
0
        public async Task <IViewComponentResult> InvokeAsync(long courseId, long?parentId)
        {
            var model = new SectionCreateVM {
                Id              = 0,
                CourseId        = courseId,
                ParentSectionId = parentId > 0 ? parentId : null
            };

            return(View(model));
        }