0

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

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

onur demir
  • 421
  • 7
  • 13
  • Look at the html your generating for the inputs - its not even close to whats is required as stated in those articles. But what is the point of this - your inputs are hidden and cant be edited - why would you generate all that extra html, send it to the client and post it all back again unchanged? –  Mar 22 '17 at 08:08
  • html is just a sample which is simplified, i use checkbox and input that shown in sample image. i add hid inputs to post display items.. For inputs i use this: `@Html.TextBoxFor(modelItem => Model.sampleModel.ElementAt(item).Value[i].Comment)` – onur demir Mar 22 '17 at 08:12
  • You cannot use `ElementAt(item)` (and look a the actual html your code generates to understand why it does not work) There are no `HtmlHelper` methods that can generate the correct html for binding to a `IDictionary` where the `Value` is a complex object. Use a view model instead. –  Mar 22 '17 at 08:15
  • Thank you very much. i have also read your solution and i give up trying to do it with Dictionary. i used nested list instead of using dictionary and it works perfect. – onur demir Mar 22 '17 at 13:13

0 Answers0