I'm making a feedback form on mvc 5 and the feedback form consists of 10 questions having 5 selected answers each. Those selected answers are the group of radio buttons that I'm populating. But in post action method, the radio buttons are not getting bind. Below is my code.
Model.cs
public class FeedbackForm
{
public string Id { get; set; }
public Models.Questionnaire mquestions { get; set; }
public List<Questionnaire> Question { get; set; }
public Batches batches { get; set; }
public User user { get; set; }
public DateTime Datetime { get; set; }
}
public class Questionnaire
{
public string QuestionId { get; set; }
public string QuestionTitle { get; set; }
[Required]
public int? SelectedAnswer { get; set; } // for binding
public IEnumerable<QuestionnaireStatus> PossibleAnswers { get; set; }
}
public class QuestionnaireStatus
{
public string StatusId { get; set; }
public string StatusTitle { get; set; }
}
Controller.cs
public ActionResult Index()
{
List<Models.FeedbackForm> lstfeedback = new List<Models.FeedbackForm>();
Models.FeedbackForm form = new Models.FeedbackForm();
Context.FeedBackForm contFeedback = new Context.FeedBackForm();
Models.FeedbackForm vm = new Models.FeedbackForm();
form.Question = new List<Models.Questionnaire>();
form.Question = contFeedback.GetAllQuestionnaire();
for (int i = 0; i < form.Question.Count; i++)
{
lstfeedback.Add(new Models.FeedbackForm
{
Question = new List<Models.Questionnaire>()
{ new Models.Questionnaire
{
QuestionId = form.Question[i].QuestionId,
QuestionTitle = form.Question[i].QuestionTitle
,
PossibleAnswers = contFeedback.GetAllStatus()
},
}
});
}
return View(lstfeedback);
}
View.cshtml
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
@*@Html.Display("Questions")*@
</th>
@for (int i = 0; i < Model.Count; i++)
{
for (int j = 0; j < Model[i].Question.Count; j++)
{
foreach (var item in Model[i].Question[j].PossibleAnswers)
{
<th>
@item.StatusTitle
</th>
}
break;
}
break;
}
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
@for (int j = 0; j < Model[i].Question.Count; j++)
{
<td>
@Html.DisplayFor(m=>Model[i].Question[j].QuestionTitle)
</td>
foreach (var item in Model[i].Question[j].PossibleAnswers)
{
<td>
@Html.RadioButtonFor(m => Model[i].Question[j].SelectedAnswer, item.StatusId, new { id = item.StatusId })
</td>
}
@Html.ValidationMessageFor(m => Model[i].Question[j].SelectedAnswer)
}
</tr>
}
</table>
<div class="form-group">
<div class="col-md-10">
<input type="submit" id="Attendance" value="Submit" class="btn btn-default" />
</div>
</div>
}