Ejemplo n.º 1
0
        public async Task <ActionResult> Create(IFormCollection collection, Questions questions, Answers answers)
        {
            try
            {
                var created = await questionsRepo.Add(questions);

                if (created == null)
                {
                    throw new Exception("Invalid Entry");
                }


                var created2 = await answersRepo.Add(answers);

                if (created2 == null)
                {
                    throw new Exception("Invalid Entry");
                }


                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create geeft error " + ex.Message);
                ModelState.AddModelError("", "Create actie is mislukt voor " + questions.QuestionID);
                return(View(questions));
            }
        }
        public async Task <ActionResult <Question> > PostQuestion([FromForm][Bind("Text", "Email", "File", "ProductId")] Question q)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (q.File != null)
            {
                var uniqueFileName = Path.GetRandomFileName().Substring(0, 8) + Path.GetExtension(q.File.FileName);

                var questionsFiles = Path.Combine(hostingEnvironment.ContentRootPath, @"Files\questionFiles");
                var filePath       = Path.Combine(questionsFiles, uniqueFileName);
                q.FileName = uniqueFileName;
                using (var stream = System.IO.File.Create(filePath))
                {
                    await q.File.CopyToAsync(stream);
                }
            }

            q.DateTimeOfCreation = DateTime.Now;

            await _repo.Add(q);

            q.File = null;
            await _hubContext.Clients.All.SendAsync("Added");

            return(Ok(q));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> AddOrUpdate(QuestionsDTO item)
        {
            setViewData();
            if (ModelState.IsValid)
            {
                if (item.Id > 0)
                {
                    await _repo.Update(item);
                }
                else
                {
                    await _repo.Add(item);
                }

                return(PartialView("_Questions", await _repo.GetList()));
            }
            return(Content("Wypełnij wszystkie wymagane pola"));
        }
        public async Task <ActionResult> Create(Questions question, Guid id)
        {
            var lijst = context.History.Where(e => e.QuizID == id);

            foreach (History history in lijst)
            {
                await historyRepo.Delete(history.HistoryID);
            }
            try
            {
                if (question.ImageString != null)
                {
                    byte[] b;
                    using (BinaryReader br = new BinaryReader(question.ImageString.OpenReadStream()))
                    {
                        b = br.ReadBytes((int)question.ImageString.OpenReadStream().Length);
                        question.ImageData = b;
                        // Convert the image in to bytes
                    }
                }

                question.QuizID     = id;
                question.QuestionID = Guid.NewGuid();

                var created = await questionsRepo.Add(question);

                if (created == null)
                {
                    throw new Exception("Invalid Entry");
                }
                return(RedirectToAction("Index", "QuestionsAnswers", new { id = id }));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create geeft error " + ex.Message);
                ModelState.AddModelError("", "Create actie is mislukt voor " + question.QuestionID);
                return(View(question));
            }
        }