Mohamed Yousef

Mohamed Yousef

  • NA
  • 40
  • 530

How can i pass multiple radio button values to controller?

May 16 2020 12:40 PM
I've a model that contains 3 tables in my view.
  1. public class InExam { public AutoTests TheTest { getset; }  
  2. public List<InTest> TheQuestions { getset; }  
  3. public IEnumerable<Result> SingleQuee { getset; } }  
First one made to get the detailed page, like "admin/AutoTests/id"
 
Second one made to get a list of questions linked to the page
 
Third one is to save radio button strings to post it back into the controller
 
my plan is to get (say) 20 questions that are linked with the detailed page, Adding 4 radio buttons for each question, and post back every selected button to the controller.
 
my view form :
  1. @using (Html.BeginForm("Test""Exams"new { id = Model.TheTest.id }, FormMethod.Post))  
  2. foreach (var item in Model.TheQuestions)  
  3. {  
  4. Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();  
  5. <div class="container" style="padding-top:50px;direction:rtl;">  
  6. <h4 style="text-align:right;font-weight:bold;">@item.Question</h4>  
  7. <div class="container">  
  8. <div class="row" style="direction:rtl;">  
  9. <div class="col-lg-7" style="text-align:right;margin-right:10px;">  
  10. <div class="row">  
  11. @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "1" })  
  12. <h5 style="padding-top:3px;padding-right:8px;">@item.RightAnswer</h5>  
  13. </div>  
  14. </div>  
  15. <div class="col-lg-7" style="text-align:right;margin-right:10px;">  
  16. <div class="row">  
  17. @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "2" })  
  18. <h5 style="padding-top:3px;padding-right:8px;">@item.Answer2</h5>  
  19. </div>  
  20. </div>  
  21. <div class="col-lg-7" style="text-align:right;margin-right:10px;">  
  22. <div class="row">  
  23. @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "3" })  
  24. <h5 style="padding-top:3px;padding-right:8px;">@item.Answer3</h5>  
  25. </div>  
  26. </div>  
  27. <div class="col-lg-7" style="text-align:right;margin-right:10px;">  
  28. <div class="row">  
  29. @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "4" })  
  30. <h5 style="padding-top:3px;padding-right:8px;">@item.Answer4</h5>  
  31. </div>  
  32. </div>  
  33. @Html.HiddenFor(m => singleQuee.Question)  
  34. </div>  
  35. </div>  
  36. </div>  
  37. }  
  38. <button class="btn botton" type="submit" onclick="return confirm('');">END</button>  
i used this line "Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();" in my view because i can't use tuple foreach ( C# ver. 5 )
 
This is my controller code :
  1. [HttpGet]public ActionResult Test(int? id)  
  2. {  
  3. using (KafoEntities db = new KafoEntities())  
  4. {  
  5. InExam model = new InExam();  
  6. model.TheTest = db.AutoTests.Where(x => x.id == id).FirstOrDefault();  
  7. model.TheQuestions = db.InTest.Where(x => x.UserEmail == currentUser.Email && x.ExamId == model.TheTest.id).OrderByDescending(x => x.id).Take(Convert.ToInt32(model.TheTest.QuestionsNumber)).ToList();  
  8. model.SingleQuee = db.Result.ToList();  
  9. return View(model);  
  10. }  
  11. }  
  12. [HttpPost]  
  13. public ActionResult Test(int? id, List<Result> singleQuee)  
  14. {  
  15. using (KafoEntities db = new KafoEntities())  
  16. {  
  17. int result = 0;  
  18. foreach (Result item in singleQuee)  
  19. {  
  20. Result sets = db.Result.Where(x => x.id == item.id).FirstOrDefault();  
  21. sets.Question = item.Question;  
  22. db.SaveChanges();  
  23. var check = db.InTest.Where(x => x.Question == item.Question).FirstOrDefault();  
  24. if (check != null)  
  25. {  
  26. if (item.Question == "1")  
  27. {  
  28. result++;  
  29. }  
  30. }  
  31. }  
  32. return RedirectToAction("Results""Exams"new { Controller = "Exams", Action = "Results", id = done.id });  
  33. }  
  34. }  
I first save the new string that came from the radio button value into the result record, then i call it back in the if condition to check it's value
 
The problem here is i get a
 
Object reference not set to an instance of an object.
 
when i post the test, it means that the list is empty, so i need to know what makes the radio buttons not working, Thanks.

Answers (3)