TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
public
class
InExam {
public
AutoTests TheTest {
get
;
set
; }
public
List<InTest> TheQuestions {
get
;
set
; }
public
IEnumerable<Result> SingleQuee {
get
;
set
; } }
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 :
@
using
(Html.BeginForm(
"Test"
,
"Exams"
,
new
{ id = Model.TheTest.id }, FormMethod.Post))
foreach
(var item
in
Model.TheQuestions)
{
Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();
<div
class
=
"container"
style=
"padding-top:50px;direction:rtl;"
>
<h4 style=
"text-align:right;font-weight:bold;"
>@item.Question</h4>
<div
class
=
"container"
>
<div
class
=
"row"
style=
"direction:rtl;"
>
<div
class
=
"col-lg-7"
style=
"text-align:right;margin-right:10px;"
>
<div
class
=
"row"
>
@Html.RadioButtonFor(x => singleQuee.Question,
new
{ @
class
=
"form-control dot"
, @Name = singleQuee.Question, @Value =
"1"
})
<h5 style=
"padding-top:3px;padding-right:8px;"
>@item.RightAnswer</h5>
</div>
</div>
<div
class
=
"col-lg-7"
style=
"text-align:right;margin-right:10px;"
>
<div
class
=
"row"
>
@Html.RadioButtonFor(x => singleQuee.Question,
new
{ @
class
=
"form-control dot"
, @Name = singleQuee.Question, @Value =
"2"
})
<h5 style=
"padding-top:3px;padding-right:8px;"
>@item.Answer2</h5>
</div>
</div>
<div
class
=
"col-lg-7"
style=
"text-align:right;margin-right:10px;"
>
<div
class
=
"row"
>
@Html.RadioButtonFor(x => singleQuee.Question,
new
{ @
class
=
"form-control dot"
, @Name = singleQuee.Question, @Value =
"3"
})
<h5 style=
"padding-top:3px;padding-right:8px;"
>@item.Answer3</h5>
</div>
</div>
<div
class
=
"col-lg-7"
style=
"text-align:right;margin-right:10px;"
>
<div
class
=
"row"
>
@Html.RadioButtonFor(x => singleQuee.Question,
new
{ @
class
=
"form-control dot"
, @Name = singleQuee.Question, @Value =
"4"
})
<h5 style=
"padding-top:3px;padding-right:8px;"
>@item.Answer4</h5>
</div>
</div>
@Html.HiddenFor(m => singleQuee.Question)
</div>
</div>
</div>
}
<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 :
[HttpGet]
public
ActionResult Test(
int
? id)
{
using
(KafoEntities db =
new
KafoEntities())
{
InExam model =
new
InExam();
model.TheTest = db.AutoTests.Where(x => x.id == id).FirstOrDefault();
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();
model.SingleQuee = db.Result.ToList();
return
View(model);
}
}
[HttpPost]
public
ActionResult Test(
int
? id, List<Result> singleQuee)
{
using
(KafoEntities db =
new
KafoEntities())
{
int
result = 0;
foreach
(Result item
in
singleQuee)
{
Result sets = db.Result.Where(x => x.id == item.id).FirstOrDefault();
sets.Question = item.Question;
db.SaveChanges();
var check = db.InTest.Where(x => x.Question == item.Question).FirstOrDefault();
if
(check !=
null
)
{
if
(item.Question ==
"1"
)
{
result++;
}
}
}
return
RedirectToAction(
"Results"
,
"Exams"
,
new
{ Controller =
"Exams"
, Action =
"Results"
, id = done.id });
}
}
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.
Reply
Answers (
3
)
how to call second page after the filled the input field?
reload and close windows