public async Task <StandardResponse> AskQuestion(ClaimsPrincipal identity, QuestionAddViewModel model, ModelStateDictionary modelState) { if (!modelState.IsValid) { return(modelState.StandardError()); } using (var db = new ServiceDb()) { using (var user = await _userService.Become(db, identity, null)) { var question = await _questionService.CreateQuestion(db, user, new CreateNewQuestion() { Title = model.Title, Body = model.Body, Tags = model.Tags, Topic = model.Topic ?? Topic.DefaultTopic }); await db.SaveChangesAsync(); return(StandardResponse.For(new { question.QuestionId })); } } }
public string AddQuestion(QuestionAddViewModel model) { Question question = new Question { Question1 = model.Question, QuestionTypeID = model.QuestionTypeID, QuestionPeriodID = model.PeriodID, Point = model.SoruPuan, Time = model.SoruSure, TopicID = model.TopicID }; QuestionBankDbContext Db = new QuestionBankDbContext(); Db.Question.Add(question); Db.SaveChanges(); List <Answers> lst = new List <Answers>(); foreach (var item in model.Answers) { Answers answers = new Answers { QuestionID = question.ID, Answer = item.AnswerContent, IsItTrue = item.Val }; lst.Add(answers); } Db.Answers.AddRange(lst); Db.SaveChanges(); return(JsonConvert.SerializeObject(new { durum = "OK", mesaj = "Soru başarıyla eklendi" })); }
public async Task <IActionResult> Add([FromBody] QuestionAddViewModel model) { try { var response = await _service.AskQuestion(User, model, ModelState); return(response.JsonResult()); } catch (Exception error) { _logger.Error(error); return(StandardResponse.ForError().JsonResult()); } }
public async Task <ActionResult> Edit(int?id) { if (id == null) { return(View("Error")); } var test = await db.Tests.FindAsync(id); if (test == null) { return(View("Error")); } var questionList = new List <QuestionAddViewModel>(); foreach (var q in test.Questions) { var question = new QuestionAddViewModel() { Id = q.Id, Text = q.Text }; question.Answers = new List <AnswerAddViewModel>(); foreach (var a in q.Answers) { question.Answers.Add(new AnswerAddViewModel() { Text = a.Text, IsCorrect = a.IsCorrect, Id = a.Id }); } questionList.Add(question); } return(View(new TestAddViewModel() { Id = test.Id, Name = test.Name, CourseId = test.Course.Id, QuestionLimit = test.QuestionLimit, Questions = questionList })); }
public ActionResult Edit(QuestionAddViewModel model) { QuestionBankDbContext Db = new QuestionBankDbContext(); Question soru = Db.Question.SingleOrDefault(x => x.ID.Equals(model.QuestionID)); soru.TopicID = model.TopicID; soru.QuestionTypeID = model.QuestionTypeID; soru.Question1 = model.Question; soru.Point = model.SoruPuan; soru.Time = model.SoruSure; List <Answers> Silinecekler = Db.Answers.Where(x => x.QuestionID.Equals(model.QuestionID)).ToList(); Db.Answers.RemoveRange(Silinecekler); List <Answers> lst = new List <Answers>(); foreach (var item in model.Answers) { Answers answers = new Answers { QuestionID = model.QuestionID, Answer = item.AnswerContent, IsItTrue = item.Val }; lst.Add(answers); } Db.Answers.AddRange(lst); Db.SaveChanges(); var modelview = new QuestionEditViewModel(soru.ID); ViewBag.Message = $"<div class='alert alert-success'><strong> Soru Başarıyla güncellendi...</strong> </div>"; return(View(modelview)); }