I have a view which outputs a list of questions. The user will select questions they want included in a report and then submit the form.
My view looks like:
@using (Html.BeginForm("Step6", "Home", FormMethod.Post))
{
@Html.HiddenFor(m => m.EventID)
@Html.HiddenFor(m => m.CompanyID)
foreach (var item in Model.Questions)
{
<div class="checkbox">
<input type="checkbox" id="@item.QuestionID" name="QuestionIds" value="@item.Title" />
<label for="@item.QuestionID">@item.Title</label>
</div>
}
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
}
Generated HTML looks like:
<div class="checkbox">
<input type="checkbox" id="12" name="QuestionIds" value="Would you like an account manager to contact you?" />
<label for="12">Would you like an account manager to contact you?</label>
</div>
<div class="checkbox">
<input type="checkbox" id="13" name="QuestionIds" value="Comments - please be as detailed as possible." />
<label for="13">Comments - please be as detailed as possible.</label>
</div>
Question collection:
public class Question
{
public Guid QuestionID { get; set; }
public string Title { get; set; }
}
Controller action
[HttpPost]
public ActionResult Step6(Models.EventCompanyQuestionnaireQuestions model)
{
return View();
}
The model:
public class EventCompanyQuestionnaireQuestions
{
public int EventID { get; set; }
public int CompanyID { get; set; }
public List<Guid> QuestionIds { get; set; }
}
When the form is submitted the List is an empty, initialized list. The rendered form element is named QuestionIds which matches the model. I need QuestionIds to have the checked checkboxes.