Skip to content
Loading
How can i pass multiple radio button values to controller?  
  • class="col-lg-7" style="text-align:right;margin-right:10px;">  
  • class="row">  
  • @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "3" })  
  • "padding-top:3px;padding-right:8px;">@item.Answer3  
  •   
  •   
  • class="col-lg-7" style="text-align:right;margin-right:10px;">  
  • class="row">  
  • @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "4" })  
  • "padding-top:3px;padding-right:8px;">@item.Answer4  
  •   
  •   
  • @Html.HiddenFor(m => singleQuee.Question)  
  •   
  •   
  •   
  • }  
  • class="btn botton" type="submit" onclick="return confirm('');">END  
  • 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 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.

    3 Replies

    Know the answer? Post it — somebody with the same question will find it here.