public ActionResult InsertData(string pitanje, string[] odgovor) { bool imaPitanje = false; TrainedQuestion trainedQuestion = new TrainedQuestion(); TrainedAnswer trainedAnswer = new TrainedAnswer(); foreach (TrainedQuestion pitanjeTr in db.TrainedQuestion) { if (pitanjeTr.TrainedQuestion1 == pitanje) { foreach (TrainedAnswer odgovorTr in db.TrainedAnswer) { for (int i = 0; i < odgovor.Length; i++) { if (odgovorTr.TrainedAnswer1 == odgovor[i]) { odgovor[i] = String.Empty; } } } imaPitanje = true; trainedQuestion = pitanjeTr; } } if (odgovor.Length > 0) { if (!imaPitanje) { trainedQuestion.TrainedQuestion1 = pitanje; trainedQuestion.TrainedQuestionId = db.TrainedQuestion.Add(trainedQuestion).TrainedQuestionId; db.SaveChanges(); } else { trainedQuestion.TrainedQuestionId = trainedQuestion.TrainedQuestionId; } for (int i = 0; i < odgovor.Length; i++) { if (odgovor[i] != "") { trainedAnswer.TrainedAnswer1 = odgovor[i]; trainedAnswer.TrainedQuestionId = trainedQuestion.TrainedQuestionId; db.TrainedAnswer.Add(trainedAnswer); db.SaveChanges(); } } } return(RedirectToAction("TrainData")); }
// POST: api/TrainedAnswer public void PostTrainedAnswer(string question, string answer) { TrainedQuestion trainedQuestion = new TrainedQuestion(); TrainedAnswer trainedAnswer = new TrainedAnswer(); trainedQuestion.TrainedQuestion1 = question; int questionId = db.TrainedQuestion.Add(trainedQuestion).TrainedQuestionId; trainedAnswer.TrainedAnswer1 = answer; trainedAnswer.TrainedQuestionId = questionId; db.TrainedAnswer.Add(trainedAnswer); db.SaveChanges(); }
private void btn_DodajPitanje_Click(object sender, EventArgs e) { try { if (txt_PitanjeInsert.Text.Length > 0 && txt_OdgovorInsert.Text.Length > 0) { TrainedQuestion trainedQuestion = new TrainedQuestion(); trainedQuestion.TrainedQuestion1 = txt_PitanjeInsert.Text; int tQuestionId = db.TrainedQuestion.OrderByDescending(x => x.TrainedQuestionId).FirstOrDefault().TrainedQuestionId + 1; db.TrainedQuestion.Add(trainedQuestion); db.SaveChanges(); if (tQuestionId > 0) { TrainedAnswer trainedAnswer = new TrainedAnswer(); trainedAnswer.TrainedAnswer1 = txt_OdgovorInsert.Text; trainedAnswer.TrainedQuestionId = tQuestionId; db.TrainedAnswer.Add(trainedAnswer); db.SaveChanges(); GenerateTrainingData(); txt_PitanjeInsert.Text = ""; txt_OdgovorInsert.Text = ""; } else { MessageBox.Show("Greška u konekciji, pokušajte ponovo!"); } } else { MessageBox.Show("Greška u konekciji, pokušajte ponovo!"); } } catch { MessageBox.Show("Greška u konekciji, pokušajte ponovo!"); } }