MVC posting with a list of objects, collection is empty
I am making a kind of walkthrough (step 1 > 2 > 3 etc) and one of the last steps is to select an item from a select list.
The view where the user selects from the select list has this model
public class EventAndCompany
{
public int EventID { get; set; }
public int CompanyID { get; set; }
}
And the view is
@model Prototypes.Models.EventAndCompany
<div class="row">
<div class="col-md-4">
<h2>Step 3. Select questionnaire(s)</h2>
@using (Html.BeginForm("Step4", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.EventID)
@Html.HiddenFor(m => m.CompanyID)
<select name="Questionnaires">
<option value="58926211-517E-4F5E-8E77-6256EC1832F6">Questionnaire 1</option>
<option value="7477ECB1-C548-4532-9D07-C01DA50A5B2C">Questionnaire 2</option>
<option value="EBE4D377-BF46-478B-AEF7-E8F853B8632C">Questionnaire 3</option>
<option value="D94B7639-AF26-4CBE-A792-DCD7AC034BFB">Questionnaire 4</option>
<option value="46544CFE-75A5-446B-8E6F-DB4E54B86890">Questionnaire 5</option>
</select>
<p><input type="submit" class="btn btn-default" value="Submit" /></p>
}
</div>
</div>
And the action, they are submitting to, has this method signature
public ActionResult Step4(Models.EventCompanyQuestionnaires model)
And that model is
public class EventCompanyQuestionnaires
{
public int EventID { get; set; }
public int CompanyID { get; set; }
public List<Questionnaire> Questionnaires { get; set; }
}
Questionnaire is
public class Questionnaire
{
public Guid QuestionnaireID { get; set; }
}
When I submit the form to Step4 action, the Questionnaires model field is initialized, but the Count is 0 and the selected option in the select is not passed to the controller.
1 answer
-
answered 2018-02-13 08:10
Saad Qais
just modify your post action to
public ActionResult Step4(Models.EventCompanyQuestionnaires model, Guid Questionnaires)