I am using ASP.NET MVC 5 and i want to pass a date from view to controller. I have done this with List but i needed to refactor my code to show the data grouped like this sample image.
I grouped my data with linq and added them to dictonary in this format: Dictionary<string, List<SampleModel>>
. I can pass the data from controller to the view without having a problem but when i try to pass it from controller to view it becomes null.
Here is how i do it in View:
@using (Html.BeginForm("SampleAction", "SampleController", FormMethod.Post, new { role = "form" }))
{
<table class="table table-bordered">
@for (int item = 0; item < Model.sampleModel.Count; item++)
{
<thead>
<tr>
<td>@Model.sampleModel.ElementAt(item).Key</td>
</tr>
</thead>
for (int i = 0; i < Model.sampleModel.ElementAt(item).Value.Count; i++)
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => Model.sampleModel.ElementAt(item).Value[i].Question)
@Html.HiddenFor(modelItem => Model.sampleModel.ElementAt(item).Value[i].Question)
</td>
</tr>
</tbody>
}
}
</table>
}
I read about mvc binders cant bind some types like List, Dictionary easily because of index names but i have done this 2using list in same way. i read couple posts which includes scoot hanselmman's and Phil Haack's but still could not done it and also dont know they are up to date. Is there any tutorial or guide about this topic that i can follow? What should i do to pass Dictionary to controller? Should i use another type instead of Dictionary?
sources: haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx