This one is a bit tricky to explain so I accept more detail may be needed.
I’m working on an MVC3 project. I have two objects which have a many-to-many relationship so I designed the models as follows:
Object1
int ID
IEnumerable<Object2> Object2s
Object2
int ID
[Required]
string Name
IEnumerable<Object1> Objects1s
I then wanted to update Object1 using a form and this included a list of checkboxes for each of Object2 in Object2s. These would be checked if an Object2 should be removed from Object2s.The view looked similar to the following:
@Html.EditorFor(c => c.ID)
if (Model.Object2s.Count() > 0)
{
@Html.EditorFor(c => c.Object2s)
}
Note – The EditorFor for c.Object2s is a custom EditorTemplate.
The problem I’ve got is when the server-side validation kicks in it reports an error because each of the Object2 objects doesn’t have its Name property set. The Name property not being important to me just to update Object1.
Did I go wrong by defining the objects with IEnumerable<Object> rather than IEnumerable<int>?
Or is there another way to stop the validation on Object2 kicking in?
OK I haven’t been able to work out exactly what I want but got around this particular problem by not binding the
Object2sproperty. Instead I am picking up anint[]of selected checkboxes in the controller and using that to populate theObject2sproperty.