0

I have some "advanced" ViewModel I cannot handle properly. After posting the form there are keys in controller's action parameters, by values are nulls. I tried everything I found and everytime it had nulls, If you need more informations just write and I'll provide them.
I'm using jQuery's select2 plugin for my select inputs.

ParentViewModel:

public class ParentViewModel
{
    public IList<string> ParentValues { get; set; }
    public IDictionary<string, IList<ChildViewModel>> ChildDictionary { get; set; }
}

ChildViewModel:

public class ChildViewModel
{
    public string Id { get; set; }
    public IList<string> ChildValues { get; set; }
}

View:

@foreach (KeyValuePair<string, IList<ChildViewModel>> child in Model.ChildDictionary)
{
    <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Values</td>
            </tr>
        </thead>
        <tbody>
        @for (var i = 0; i < child.Value.Count; i++)
        {
        <tr>
            <td>@child.Value[i].Id</td>
            <td>
                @Html.HiddenFor(m => child.Value[i].Id)
                // Here i tried manual binding
                <select multiple="multiple" name="Model.ChildDictionary[@child.Key].Value[@i].ChildValues">
                @for (var r = 0; r < Model.ParentValues.Count; r++)
                {
                    <option @(child.Value[i].ChildValues.Contains(Model.ParentValues[r]) ? "selected" : "") value="@Model.ParentValues[r]">
                        @Model.ParentValues[r]
                    </option>
                }
                </select>
            </td>
        </tr>
        }
    </tbody>
    </table>
}

EDIT: Ok, if dictionary is bad, maybe you have an idea.
I got a List of that type of data:

ID                               | VALUES
Base1-subbase1-foo | foo, bar
Base1-subbase2-foo | foo
Base2-subbase1-foo | foo, bar
Base2-subbase2-foo | foo, bar
Base3-subbase1-foo | bar

and I need to group them by the "Base*" id value

mazxaxz
  • 1
  • 1
  • 1
    Suggest you read [this answer](https://stackoverflow.com/questions/41258733/bind-dictionary-with-list-in-viewmodel-to-checkboxes/41560929#41560929) - in short, do not use a `Dictionary` –  Sep 14 '18 at 12:45
  • In any case, you cannot use ` foreach` loop to generate form controls for a collection (your `name` attributes have no relationship to the model) - refer [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) –  Sep 14 '18 at 12:47
  • Well, in this specific case i need the dictionary to render view properly. I tried to post FormCollection as a action parameter, but I got only keys without values, is it related to the usage of dictionary? – mazxaxz Sep 14 '18 at 13:07
  • No you don't need a `Dictionary` - you need view model to represent what you want in the view (did you even read the links I gave you?) - Your `ParentViewModel` will contain a `List Children` proeprty where `XViewModel` contain a `string` property and the `List` property and you will use `for` loops or `EditorTemplates` to generate the view. But your models and view do not make sense for a dropdownlist anyway (for that you have a `IEnumerable` property and use `@Html.DropDownListFor()`, so I have no idea what you are trying to do with this code) –  Sep 14 '18 at 13:14
  • Based on your last edit you have said _I need to group them by the Base id_ - therefore you use a query with a `GroupBy()` clause –  Sep 14 '18 at 13:24
  • Thanks for your help, I figured it out with crazy 20 line long query :) – mazxaxz Sep 14 '18 at 17:23

0 Answers0