I have 2 models inside my asp.net mvc-5 web application:-
public partial class Anwer
{
public Anwer()
{
this.UserFormsAnswers = new HashSet<UserFormsAnswer>();
}
public int Id { get; set; }
public string AnwerDesc { get; set; }
public int QuestionID { get; set; }
public bool Correct { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<UserFormsAnswer> UserFormsAnswers { get; set; }
}
public partial class Question
{
public Question()
{
this.Anwers = new HashSet<Anwer>();
}
public int Id { get; set; }
public string QuestionDesc { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public virtual ICollection<Anwer> Anwers { get; set; }
}
now i want to show all the questions inside the view and for each question to show its answers in a radio buttons, so i tried the following:-
@model IEnumerable<QuestionsDemo.Models.Question>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@{int i = 0;
foreach (var item in Model)
{
<div class="form-group">
@(i+1) <spn>-</spn> @Html.DisplayFor(modelItem => item.QuestionDesc) <br />
@foreach (var answer in item.Anwers)
{
<div class="col-md-10">
@Html.RadioButtonFor(modelitem2 => answer.QuestionID, answer.Id) @answer.AnwerDesc <br />
</div>
}
</div><hr/>
}
}
</div>
}
I thought that all the answers' radio buttons for a question will have the same name. so users can only select one option for each question. but seems all the answers radio-buttons got the same name as follow:-
<input id="answer_QuestionID" name="answer.QuestionID" type="radio" value="1" /> 10 <br />
so can any one adivce on this ? how i can group the radio buttons per question?
second question. how i can post back all the answers ids to my post action method? should i use indexer ??