0

let's say I use Entity Framework 6 and have the following two tables in a model-first approach:

enter image description here

This creates following code:

enter image description here

enter image description here

My question is now, how do I use the IEnumeration properties in a Create View? In a Details or Delete View, I just iterate through them using Html.DisplayFor helper, but I don't know what I could use to achieve the comparable for Create and Edit Views. Something like this:

<div class="form-group">
    @Html.LabelFor(model => model.MappedEmployees, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <!--Something like checkbox list with Name property of each collection property item-->
    </div>
</div>

So I would end up with something like:

enter image description here

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76
  • 1
    This is more of an MVC razor question than EF. – Steve Greene Jan 23 '16 at 18:34
  • Are you wanting to display all employees and then use a checkbox to select the ones associated with Account? –  Jan 27 '16 at 03:33
  • yes, that is what I am trying to achieve... – LeonidasFett Jan 27 '16 at 08:32
  • Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example (instead or User and Roles, it would be Account and Employees) –  Jan 27 '16 at 23:13

1 Answers1

0

There's a few good blog posts on this subject, they should be useful for you in this area:

haacked.com blog posting

Hanselman.com blog posting

I've not tested the below, but something like this:

<div class="form-group">
    @Html.LabelFor(model => model.MappedEmployees, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">

@for(int i = 0; i < model.MappedEmployees.Count; i ++)
{
Html.CheckBoxFor(model => model.MappedEmployees[i],  new { htmlAttributes = new { @class = "form-control" } })
}

    </div>
</div>
Tim Snow
  • 345
  • 1
  • 4
  • it seems that I need a bool value for the expression parameter of the CheckBoxFor constructor. I don't know however how to get it working so that it can add/remove items from my ICollection property. – LeonidasFett Jan 23 '16 at 19:00