Hi, I have a controller with a method:

[HttpPost] [Route(FirstStepFilledRoute)] public IActionResult OnFirstStepFilled( PdoResultViewModel pdoResultViewModel, int id) {} 

And the form that this controller pulls:

 @model WebApp.Models.PdoResultViewModel <form asp-route="@Model.Route" method="post"> @for(int i = 0; i < Model.PdoQuestionSetViewModels.Length; i++){ <div class="panel panel-primary"> <div class="panel-heading"> @Model.PdoQuestionSetViewModels[i].Value </div> <div class="panel-body"> @for(int j = 0; j < Model.PdoQuestionSetViewModels[i].PdoQuestionAnswerViewModels.Length; j++){ <div class="checkbox"> <label> @Html.HiddenFor(m => m.PdoQuestionSetViewModels[i].PdoQuestionAnswerViewModels[j].Id) @Html.CheckBoxFor(m => m.PdoQuestionSetViewModels[i].PdoQuestionAnswerViewModels[j].IsChecked) @(j+1). @Model.PdoQuestionSetViewModels[i].PdoQuestionAnswerViewModels[j].Value </label> </div> } </div> </div> } <input type="submit" class="btn btn-primary" value="Далее" /> </form> 

Model:

 public sealed class PdoResultViewModel { public int Id { get; set; } public PdoQuestionSetViewModel[] PdoQuestionSetViewModels { get; set; } public string Message { get; set; } public string Route { get; set; } } public sealed class PdoQuestionSetViewModel { public string Value { get; set; } public PdoQuestionAnswerViewModel[] PdoQuestionAnswerViewModels { get; set; } } public sealed class PdoQuestionAnswerViewModel { public int Id { get; set; } public string Value { get; set; } public bool IsChecked { get; set; } } 

When submitting a form, I successfully get into my controller method. But part of the data does not come: pdoResultViewModel.PdoQuestionSetViewModels is always null. I know for sure that the data is sent by Request.Form. contains the required number of completed KeyValuePair.

Something like the following shows firefox console:

 ... PdoQuestionSetViewModels[0].PdoQuestionAnswerViewModels[0].Id:"15728" PdoQuestionSetViewModels[0].PdoQuestionAnswerViewModels[0].IsChecked:"false" ... 

What could I have done wrong? Why can't the controller parse a complex model?

    1 answer 1

    Without waiting for an answer, I rewrote to an easier view model: now I send the list {id: number, isChecked: bool} to the controller, and everything else in the view-bag.

    In general, it seems that either the form builds json in the wrong format, or some kind of magic with attributes was needed, or the presence of a designer or some method in the model confuses reflection.