I'm making a survey application. Survey has Questions and Questions have QuestionOption. haveThere is a example here. I am trying to use this technique in a large form with a list(List) but when I post back, the Viewmodel.Order that should’ve contained list of items and activities return with the lists empty.
My QuestionModel.cs like this.
public int Id { get; set; }
public string QuestionText { get; set; }
public System.Nullable<bool> OptionType1 { get; set; }
public System.Nullable<bool> OptionType2 { get; set; }
public List<QuestionOptionModel> OptionList = new List<QuestionOptionModel>();
When I post back "IEnumerable questions" List OptionList comes null. How can I do this?
public ActionResult CallSurvey()
{
IEnumerable<QuestionModel> questionModelList = (IEnumerable<QuestionModel>)SessionHelper.GetSessionObject(SessionKeys.SurveyKey);
questionModelList = questionSrv.GetQuestionModel();
return View(questionModelList);
}
questionModelList include all my survey question and question options. When I post it, post back is coming with only null optionList.
[HttpPost]
public ActionResult CallSurvey(IEnumerable<QuestionModel> questions)
{ ..... }
CallSurvey.cshtml
<body>
@using ((Html.BeginForm()))
{
@ViewBag.Test
<section class="slides layout-regular template-kendo">
@foreach (var item in Model)
{<article>
@Html.Partial("QuestionEditor", item)
</article>
}
<div class="slide-area" id="prev-slide-area"></div>
<div class="slide-area" id="next-slide-area"></div>
</section>
}
</body>
QuestionEditor.cshtml
@model LSMM.Business.Model.Survey.QuestionModel
@using LSMM.Web.Helpers
<div>
@using (Html.BeginCollectionItem("Questions"))
{
<table id="table1">
<tr>
<td>
<div id="@Model.Id" class="hint">
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.QuestionText)
@Html.HiddenFor(m => m.OptionType1)
@Html.HiddenFor(m => m.OptionType2)
@for (int i = 0; i < Model.OptionList.Count; ++i)
{
@Html.LabelFor(m => m.OptionList[i].Id)
@Html.LabelFor(m => m.OptionList[i].QuestionId)
@Html.LabelFor(m => m.OptionList[i].Checked)
@Html.LabelFor(m => m.OptionList[i].Description)
@Html.LabelFor(m => m.OptionList[i])
}
<span id="sorular">@Model.Id. @Model.QuestionText</span>
<br />
<br />
</div>
</td>
</tr>
<tr>
<td>
<div class="hint2">
@Html.Partial("QuestionOptionEditor", Model)
</div>
</td>
<td>
<div id="@Model.Id-Img">
<h2 style="top: 200px; right: 0;">
<img src="../../Content/css/img/@Model.Id-Img.png"></h2>
</div>
</td>
</tr>
</table>
and QuestionOptionEditor.cshtml
@model LSMM.Business.Model.Survey.QuestionModel
@using LSMM.Web.Helpers
@foreach (var option in @Model.OptionList)
{
<p>
@if (@Model.OptionType1 == false)
{
@Html.Partial("QuestionOptionModel", option)
}
else
{
@Html.Partial("../Shared/DisplayTemplates/QuestionOptionModel", option)
}
</p>
}
Here QuestionOptionModel views like this;
@model LSMM.Business.Model.Survey.QuestionOptionModel
@(Html.RadioButtonFor(m => m.Id, true, new { Id = @Model.Id, Name = @Model.QuestionId })) @Html.Label("Evet")
<br />
<br />
@(Html.RadioButtonFor(m => m.Id, false ,new { Id=@Model.Id, Name = @Model.QuestionId})) @Html.Label("Hayır")