How do I create a selection list in ASP.NET MVC?
- by Gary McGill
I have a database table that records what publications a user is allowed to access. The table is very simple - it simply stores user ID/publication ID pairs:
CREATE TABLE UserPublication (UserId INTEGER, PublicationID INTEGER)
The presence of a record for a given user & publication means that the user has access; absence of a record implies no access.
I want to present my admin users with a simple screen that allows them to configure which publications a user can access. I would like to show one checkbox for each of the possible publications, and check the ones that the user can currently access. The admin user can then check or un-check any number of publications and submit the form.
There are various publication types, and I want to group the similarly-typed publications together - so I do need control over how the publications are presented (I don't want to just have a flat list).
My view model obviously needs to have a list of all the publications (since I need to display them all regardless of the current selection), and I also need a list of the publications that the user currently has access to. (I'm not sure whether I'd be better off with a single list where each item includes the publication ID and a yes/no field?).
But that's as far as I've got. I've really no idea how to go about binding this to some checkboxes. Where do I start?