I'm currently editing a custom object, which has a dictionnary Dictionary<MyOtherObject,Boolean>
.
public class MyObject{
public int SomeProperties{get;set;}
public int SomeMoreProperties{get;set;}
....
public Dictionary<MyOtherObject, Boolean> Attributions{get;set;}
}
The goal is that I can have a list of checkbox for every item the dictionary, and decide to check it or not.
I've tried to make and editor template for my "Attribution" property. It displays perfectly, taking in account which values have been selected. But when I submit, I receive a null for the Dictionary.
So what is the good way to do it?
I found this stack overflow post which has almost the same problem, instead that I've a Dictionary and not simple properties. How well MVC handle them?
I can transform my data, but I don't see a way to not have a "List" of element. Also found this but no answers
EDIT My colleague did made some tries, starting to display only a list of bool instead of a list of objects: With an object
public class TheViewObject{
//other fields
List<bool> MyList{get;set;}
}
In the view, if we generate the form with @Html.EditorFor(m=>m.MyList)
, we got this:
<input checked="checked" class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList_0_" name="MyList[0]" value="true" type="checkbox">
<input class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList_1_" name="MyList[1]" value="true" type="checkbox">
<input checked="checked" class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList_2_" name="MyList[2]" value="true" type="checkbox">
<input name="MyList[0]" value="false" type="hidden"><input name="MyList[1]" value="false" type="hidden"><input name="MyList[2]" value="false" type="hidden">
It works because it knows directly how to handle a List<bool>
. This code works and we receive correct data on the form return.
Now if we have a template
@model List<bool>
@for (int i = 0; i < Model.Count; i++) {
@Html.EditorFor(x => Model[i])
}
And the result:
<input checked="checked" class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList__0_" name="MyList.[0]" value="true" type="checkbox">
<input name="MyList.[0]" value="false" type="hidden">
<input class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList__1_" name="MyList.[1]" value="true" type="checkbox">
<input name="MyList.[1]" value="false" type="hidden">
<input checked="checked" class="check-box" data-val="true" data-val-required="The Boolean field is required." id="MyList__2_" name="MyList.[2]" value="true" type="checkbox">
<input name="MyList.[2]" value="false" type="hidden"> </td>
The result is basically the same, EXCEPT, that we have an additional "." between MyList and [x]. With this synthax the model binder cannot figure out how to bind the object.
I think this is basically what is also happening with our Dictionary before(we have tested with a list instead, same problem)
(I'm currently testing Steve Sanderson's begincollectionitem)