public void AddQuestionToTest(int questionId, int testId, DbContext dbCtx) { QuestionService qservice= new QuestionService(dbCtx); TestService tservice = new TestService(dbCtx); Question q = qservice.Find(questionId); Test t = tservice.Find(testId); t.Questions.Add(q); q.TestsList.Add(t); qservice.QuestionManager.Update(q); tservice.TestManager.Update(t); }
// This is the Insert method to insert the entered TestSetup item // USAGE: <asp:FormView InsertMethod="InsertItem"> public void InsertItem() { using (_db) { var testDropdown = formviewID.FindControl("testDrpdwn") as DropDownList; var item = new Authorization_App.Model.TestSetup(); if (ViewState["expDate"]!=null) { item.ExpiresAt = (DateTime)ViewState["expDate"]; } //We use System.Guid to generate a random string. The parameter "N" is used to set the format. //It means 32 digits without hyphens and braces. string rand32digString = System.Guid.NewGuid().ToString("N"); //We will need only the first 8 digits of this string. item.Code = rand32digString.Substring(0,8); string userId = HttpContext.Current.User.Identity.GetUserId(); item.AuthorId = userId; var selectedTest = testDropdown.SelectedItem; TestService testService = new TestService(_db); Model.Test t =testService.Find(selectedTest.Text); if(t!=null) { item.TestId = t.Id; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes _db.TestSetup.Add(item); _db.SaveChanges(); Response.Redirect("Default"); } } }