Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass List of Checkboxes into View and Pull out IEnumerable [duplicate]

Tags:

I have a list of items that will be associated to a user. It's a one-to-many relationship. I want the entire list of items passed into the view so that they can choose from ones that are not associated to them yet (and also see those that are already associated). I want to create checkboxes from these. I then want to send the selected ones back into the controller to be associated. How can I pass in the list of all of them, including those that aren't yet associated, and reliably pass them back in to be associated?

Here's what I tried first, but it's clear this won't work as I'm basing the inputs off the items passed in via the AllItems collection, which has no connection to the Items on the user itself.

<div id="item-list">
    @foreach (var item in Model.AllItems)
    {
        <div class="ui field">
            <div class="ui toggle checkbox">
                <input type="checkbox" id="[email protected]" name="Items" value="@item.Active" />
                <label for="[email protected]">@item.ItemName</label>
            </div>
        </div>
    }
</div>