Add/remove a list of items for a single object
For my site, I have a list of users and a list of events. I will be assigning users to each event. I have a table named EventUsers to hold this data:
------------------------
EventUsers
------------------------
EventID | UserID
------------------------
1 | 123
1 | 456
2 | 789
On my page, I want to be able to add and remove users for a certain event.
I could have a dropdown populated with users and an "Add" button next to it, then when I want to add a user, I pick one and click "Add". There would be a grid below it displaying all the users for the event, with "Delete" buttons for each one. The downside to this is that a database call is made for every Add and Delete.
Another option is to have two listboxes on the page - one on the left containing all users in the database, and another on the right which will contain users for the given event, and I can just add or remove from the list on the right. Then when I'm done, I click save, and it makes one database call. The only issue is that if I'm removing 开发者_如何学Cand adding, I'll have to delete every record in my EventUsers table for that event, and then add the changes.
I've run into this before, and I've always gone with the dropdown method. What's the preferred method here?
Neither is perfect. In these situations it's best to do some user testing, and worry about the technical implementation later ;)
From a user journey point of view, if I'm adding a lot of Users to an Event I'd prefer it to be quick and not have a Postback per action.
To me it sounds like you're worried a bit too much about the database hits in the first solution. Inserting or deleting one tiny record from that table is really marginal, in my opinion. Even on a large site, the database hits aren't really going to matter unless you've got a clustered index somewhere that could slow things down.
If you don't like the database hits (again, I think these are marginal), and you don't like removing/re-adding records, you could consider storing the changes in the session and not persisting them until a "Save" button is clicked. This requires a bit more action by your end user, however, so it's probably not the greatest idea, either.
Either way should work but it sounds like your second option of using the list on the right to add/delete items is the way to go since you have already done it and the technique is used in many other situations so is familar to users.
精彩评论