public void SaveExcerciseAnswer(Exercise excercise) { var answer = new SysAnswer { QuestionId = excercise.QuestionId, Answer = excercise.FirstNumber + excercise.SecondNumber }; Answers.Add(answer); }
public ActionResult Create(string questions, int userId, string title) { List<QuestionModel> qmodels = JsonConvert.DeserializeObject<List<QuestionModel>>(questions); Questionnaire Questionnaire = new Questionnaire { Title = title, UserID = userId, Time = DateTime.Now }; db.Questionnaires.Add(Questionnaire); int result = 0; foreach (var model in qmodels) { Question question = new Question { QuestionnaireID = Questionnaire.ID, Title = model.Title, Time = DateTime.Now, IsBlank = false, IsMulSelect = false }; db.Questions.Add(question); SysAnswer sysAnswer1 = new SysAnswer(); sysAnswer1.Content = model.Q1; sysAnswer1.QuestionID = question.ID; SysAnswer sysAnswer2 = new SysAnswer(); sysAnswer2.Content = model.Q2; sysAnswer2.QuestionID = question.ID; SysAnswer sysAnswer3 = new SysAnswer(); sysAnswer3.Content = model.Q3; sysAnswer3.QuestionID = question.ID; SysAnswer sysAnswer4 = new SysAnswer(); sysAnswer4.Content = model.Q4; sysAnswer4.QuestionID = question.ID; db.SysAnswers.Add(sysAnswer1); db.SysAnswers.Add(sysAnswer2); db.SysAnswers.Add(sysAnswer3); db.SysAnswers.Add(sysAnswer4); result= db.SaveChanges(); } if (result > 0) { var context = GlobalHost.ConnectionManager.GetHubContext<QuestionnarieHub>();//得到Signalr context context.Clients.All.GetNew(Questionnaire); //将新上传的资源广播到全部客户端 return Content("ok"); } else { return Content("fail"); } }