public async Task <IActionResult> Edit(int id, [Bind("StackId,Stack,Type,Timestamp")] Questionstacks questionstacks)
        {
            if (id != questionstacks.StackId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(questionstacks);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!QuestionstacksExists(questionstacks.StackId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(questionstacks));
        }
        public async Task <IActionResult> Create([Bind("StackId,Stack,Type,Timestamp")] Questionstacks questionstacks)
        {
            if (ModelState.IsValid)
            {
                _context.Add(questionstacks);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(questionstacks));
        }
        // GET: QuestionsPreviewFromStack/BuildStack/5
        public ActionResult BuildStack(int id)
        {
            try
            {
                var stackQuery = _context.Questionstacks.Where(sq => sq.StackId == id)
                                 .Select(sq => new Questionstacks
                {
                    Stack = sq.Stack, StackId = sq.StackId, Type = sq.Type, Timestamp = sq.Timestamp
                });

                if (!stackQuery.Any())
                {
                    return(NotFound());
                }

                Questionstacks questionstack = stackQuery.FirstOrDefault() ?? new Questionstacks();

                short totalQuestions = 0;
                totalQuestions = questionstack.MaximumQuestionNumberPerStack;

                _context.Questionstackitems.RemoveRange(_context.Questionstackitems.Where(x => x.StackId == id));

                for (int i = 1; i <= totalQuestions; i++)
                {
                    string MapType    = MappingType.ToString();
                    int    difficulty = _context.Qleveldifficultymaping.FirstOrDefault(x => x.Level == i && x.Maping == MapType).Difficulty;
                    int    type       = _context.Qleveldifficultymaping.FirstOrDefault(x => x.Level == i && x.Maping == MapType).Type;
                    if (questionstack.StackType == QuestionTypeDescription.Qualification)
                    {
                        difficulty = ((i + 1) % 2) + 1;                                                                 //alternate 1-2-1-2
                    }
                    long RndQuestionId              = -1;
                    int  RndCategoryId              = -1;
                    int  RndSubcategoryId           = -1;
                    int  RndAdditionalCategoryId    = -1;
                    int  RndAdditionalSubcategoryId = -1;

                    Gamequestions randomquestion = null;
                    if (UseNewQuestions)
                    {
                        randomquestion = _context.GetRandomlySelectedQuestion(id, type, difficulty, 0);
                    }

                    if (UseOldQuestions && randomquestion == null)
                    {
                        randomquestion = _context.GetRandomlySelectedQuestion(id, questionstack.Type, difficulty, 1);
                    }

                    if (randomquestion != null)
                    {
                        RndQuestionId              = randomquestion.QuestionId;
                        RndCategoryId              = randomquestion.CategoryId;
                        RndSubcategoryId           = randomquestion.SubcategoryId;
                        RndAdditionalCategoryId    = randomquestion.AdditionalCategoryId ?? -1;
                        RndAdditionalSubcategoryId = randomquestion.AdditionalSubcategoryId ?? -1;
                    }

                    _context.Questionstackitems.Add(new Questionstackitems
                    {
                        StackId                 = id,
                        StackLevel              = i,
                        QuestionId              = RndQuestionId,
                        CategoryId              = RndCategoryId,
                        SubcategoryId           = RndSubcategoryId,
                        AdditionalCategoryId    = RndAdditionalCategoryId,
                        AdditionalSubcategoryId = RndAdditionalSubcategoryId
                    });
                    _context.SaveChanges();
                }
                TempData["SuccessMessage"] = "Stack is built successfully";
            }
            catch (Exception e)
            {
                TempData["ErrorMessage"] = e.StackTrace ?? "Error";
            }

            return(RedirectToAction(nameof(Index), new { id }));
        }