I'm new to ASP .Net Core 1.0 and I'm trying to bind a view to a model that contains a collection of same object. The main model is a questionnaire and may contain any count of questions with different types. But my problem is inside the ActionResult method, my List property is always null during post.
This is the form:
<form id="mainForm" asp-controller="Default" asp-action="PersonalDetails" asp-route-sectionTypeCode="@Model.SectionTypeCode" asp-route-sectionNumber="@Model.SectionNumber" method="post" asp-anti-forgery="false">
@{
for (Int32 i = 0; i < Model.Questions.Count; i++)
{
string spacerValue = "";
if (i == Model.Questions.Count - 1)
{
spacerValue = "extraLargeSpacerBottom";
}
<div class="form-group @spacerValue">
<label>@Model.Questions[i].QuestionTitle</label>
@{
switch (Model.Questions[i].QuestionTypeCode)
{
case 1:
<input asp-for="Questions[i].Value" class="form-control appInput" type="text">
break;
case 2:
<input asp-for="Questions[i].Value" class="form-control appInput" type="number">
break;
case 3:
<select asp-for="Questions[i].Value" class="form-control appInput" asp-items="Model.Questions[i].QuestionOptions"></select>
break;
case 4:
<input asp-for="Questions[i].Value" class="form-control appInput" type="date">
break;
case 5:
<div>
<label class="radio-inline">
<input asp-for="Questions[i].Value" value="true" type="radio"><span class="radioSpace">Yes</span>
</label>
<label class="radio-inline">
<input asp-for="Questions[i].Value" value="false" type="radio"><span class="radioSpace">No</span>
</label>
</div>
break;
case 6:
<input asp-for="Questions[i].Value" class="form-control appInput" type="number">
break;
}
}
</div>
}
}
</form>
And this is the _Question class:
public class _Question
{
#region Properties
public string QuestionTitle { get; private set; }
public string QuestionHelp { get; private set; }
public Int32 QuestionTypeCode { get; private set; }
public string ValidationMessage { get; private set; }
public List<SelectListItem> QuestionOptions { get; private set; }
public dynamic Value { get; set; }
#endregion
public _Question() { }
public _Question(Int32 questionTypeCode, string questionTitle, string questionHelp, List<SelectListItem> options = null)
{
QuestionTypeCode = questionTypeCode;
QuestionTitle = questionTitle;
QuestionHelp = questionHelp;
QuestionOptions = options;
switch (questionTypeCode)
{
case 1:
ValidationMessage = "Please enter a text value";
break;
case 2:
ValidationMessage = "Please enter a numeric value";
break;
case 4:
ValidationMessage = "Please enter a date value";
break;
case 6:
ValidationMessage = "Please enter a currency value";
break;
}
}
}
The main model contains a list of _Question objects that are being generated at runtime from a database.
Can anybody please help me about my null result?